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;
}
}

最新文章

  1. Screen Orientation for Windows Phone
  2. 使用rsync和scp远程同步文件
  3. Linux命令小结:crontab/netstat/iostat/sar
  4. 第18章 图元文件_18.2 增强型图元文件(emf)(1)
  5. android自定义view仿照MIUI中音量控制效果
  6. GO语言中间的derfer
  7. Win7在CMD命令行中使用管理员权限运行命令
  8. Mechanism of Loading Resources
  9. .NET MVC权限控制
  10. Android创建与读取Excel
  11. Html基础详解
  12. Thinkphp5+PHPExcel实现批量上传表格数据功能
  13. 1.openshift
  14. 无法将文件“..\bin\Debug \**.dll”复制到“bin\**.dll”。对路径“bin \**.dll”的访问被拒绝。
  15. 微信公众号Java接入demo
  16. Git Extensions system.invalidoperationexception尚未提供文件名,因此无法启动进程
  17. High level GPU programming in C++
  18. ubuntu16.04通过apt-get方式安装MongoDB
  19. CentOS之Shell基础
  20. JAVA框架 Spring JDBC模板

热门文章

  1. ironic baremetal node status
  2. [0] OpenCV_Notes - 琐碎
  3. wireshark简单使用
  4. android桌面悬浮窗实现
  5. KVC 开发详情
  6. 【bzoj2959】长跑 LCT+并查集
  7. linux shell常用语法
  8. 熊猫(i)
  9. 方伯伯的OJ ( onlinejudge )
  10. php中json_encode和json_decode的用法