将一个txt里的A和B谈话内容获取出来并分别保存到A和B的txt文件中
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.io.FileNotFoundException;
public class communication {
public static void main(String[] args){
int i=0,j=0;
String content="GBK";
String filePath ="d:/lgq.txt";
readTxtFile(filePath);
String person1 = "A:";
String fileName1 ="D:/A.txt";
String fileName2 ="D:/B.txt";
String[] arrs1= new String[30];
String[] arrs2= new String[30];
String str1 = new String(); //原有txt内容
try {
String encoding=content;
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
if (lineTxt.contains(person1)){
arrs1[i]=lineTxt;
i++;
}else {
arrs2[j]=lineTxt;
j++;
}
}
System.out.println(Arrays.toString(arrs1));
System.out.println(Arrays.toString(arrs2));
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
try {
File f1 = new File(fileName1);
createFile(fileName1);
// 建立输出字节流
FileWriter fos = null;
try {
fos = new FileWriter(f1);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 用FileWriter 的write方法写入字节数组
try {
for(int tmp1=0;tmp1<arrs1.length;tmp1++){
if(arrs1[tmp1]!=null){
fos.write(arrs1[tmp1]);
fos.write("\r\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("写入成功");
// 为了节省IO流的开销,需要关闭
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}catch (IOException e){
e.printStackTrace();
}
System.out.println(str1);
try {
File f2 = new File(fileName2);
createFile(fileName2);
// 建立输出字节流
FileWriter fos2 = null;
try {
fos2 = new FileWriter(f2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 用FileOutputStream 的write方法写入字节数组
try {
for(int tmp2=0;tmp2<arrs1.length;tmp2++){
if(arrs2[tmp2]!=null){
fos2.write(arrs2[tmp2]);
fos2.write("\r\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("写入成功");
// 为了节省IO流的开销,需要关闭
try {
fos2.close();
} catch (IOException e){
e.printStackTrace();
}
}
catch (IOException e){
e.printStackTrace();
}
// String a=new String [i];
// for( j=0;j<i;j++){
// if((String a[i]= bufferedReader.readLine())){
// }
// }
// System.out.println(Arrays.toString(a));
//创建目录
// String dirName = "D:/work/temp/temp0/temp1";
// String dirName = "D:";
// communication.createDir(dirName);
//创建文件
// String fileName = dirName + "/temp2/tempFile.txt";
//创建临时文件
// String prefix = "temp";
/*
* String suffix = ".txt";
* for (int i = 0; i < 10; i++) {
* System.out.println("创建了临时文件:"
* + communication.createTempFile(suffix, dirName));
* }
* //在默认目录下创建临时文件
* for (int i = 0; i < 10; i++) {
* System.out.println("在默认目录下创建了临时文件:"
* + communication.createTempFile(p suffix, null));
* }
*/
}
/**
* 读TXT文件内容
* @param fileName
* @return
*/
public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
/**
* 创建文件
* @param fileName
* @return
*/
public static boolean createFile(String filename) {
File file = new File(filename);
if(file.exists()) {
System.out.println("创建单个文件" + filename + "失败,目标文件已存在!");
return false;
}
if (filename.endsWith(File.separator)) {
System.out.println("创建单个文件" + filename + "失败,目标文件不能为目录!");
return false;
}
//判断目标文件所在的目录是否存在
if(!file.getParentFile().exists()) {
//如果目标文件所在的目录不存在,则创建父目录
System.out.println("目标文件所在目录不存在,准备创建它!");
if(!file.getParentFile().mkdirs()) {
System.out.println("创建目标文件所在目录失败!");
return false;
}
}
//创建目标文件
try {
if (file.createNewFile()) {
System.out.println("创建单个文件" + filename + "成功!");
return true;
} else {
System.out.println("创建单个文件" + filename + "失败!");
return false;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("创建单个文件" + filename + "失败!" + e.getMessage());
return false;
}
}
public static boolean createDir(String destDirName) {
File dir = new File(destDirName);
if (dir.exists()) {
System.out.println("创建目录" + destDirName + "失败,目标目录已经存在");
return false;
}
if (!destDirName.endsWith(File.separator)) {
destDirName = destDirName + File.separator;
}
//创建目录
if (dir.mkdirs()) {
System.out.println("创建目录" + destDirName + "成功!");
return true;
} else {
System.out.println("创建目录" + destDirName + "失败!");
return false;
}
}
public static boolean writeTxtFile(String content,File fileName)throws Exception{
RandomAccessFile mm=null;
boolean flag=false;
FileOutputStream o=null;
try {
o = new FileOutputStream(fileName);
o.write(content.getBytes("GBK"));
o.close();
// mm=new RandomAccessFile(fileName,"rw");
// mm.writeBytes(content);
flag=true;
} catch (Exception e) {
e.printStackTrace();
}finally{
if(mm!=null){
mm.close();
}
}
return flag;
}
}