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

将一个txt里的A和B谈话内容获取出来并分别保存到A和B的txt文件中的更多相关文章

  1. File操作-将txt里的内容写入到数据库表

    package com.Cristin.File;//将txt里的内容写入到数据库表 import com.Cristin.MySQL.AddDataToDB;import org.testng.an ...

  2. cmd命令行结果保存到txt里,屏幕显示一行就保存一行到txt

    #coding:utf-8 """ 1.重定向print 2.python与cmd命令 """ import sys import os i ...

  3. 使用po模式读取豆瓣读书最受关注的书籍,取出标题、评分、评论、题材 按评分从小到大排序并输出到txt文件中

    #coding=utf-8from time import sleepimport unittestfrom selenium import webdriverfrom selenium.webdri ...

  4. SQL C# nvarchar类型转换为int类型 多表查询的问题,查询结果到新表,TXT数据读取到控件和数据库,生成在控件中的数据如何存到TXT文件中

    在数据库时候我设计了学生的分数为nvarchar(50),是为了在从TXT文件中读取数据插入到数据库表时候方便,但是在后期由于涉及到统计问题,比如求平均值等,需要int类型才可以,方法是:Conver ...

  5. 保存文件名至txt文件中,不含后缀

    准备深度学习的训练数据时,可能会用到将图片文件名保存到txt文件中,所以用python实现了该功能.输入参数只设了两个,图片存放路径,和输出的txt文件名. 代码里写死了只识别.jpg格式,并不进行目 ...

  6. 多个.txt文件合并到一个.txt文件中

    如果想要将多个.txt文件合并到一个.txt文件中,可以先将所有.txt文件放到一个文件夹中,然后使用.bat文件完成任务. 例如,在一个文件夹下有1.txt, 2.txt, 3.txt三个文件,想把 ...

  7. java:利用java的输入/输出流将一个文件的每一行+行号复制到一个新文件中去

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  8. python : 将txt文件中的数据读为numpy数组或列表

    很多时候,我们将数据存在txt或者csv格式的文件里,最后再用python读取出来,存到数组或者列表里,再做相应计算.本文首先介绍写入txt的方法,再根据不同的需求(存为数组还是list),介绍从tx ...

  9. pandas 从txt读取DataFrame&DataFrame格式化保存到txt

    前提 首先保证你txt里的文本内容是有规律可循的(例如,列与列之间通过“\t”.“,”等指定的可识别分隔符分隔): 例如我需要读取的数据,(\t)分隔: (此文件内容是直接以DataFrame格式化写 ...

随机推荐

  1. Percona-Tookit工具包之pt-summary

      Preface       As a dba,We are obliged to master several basic tools(such as vmstat,top,netstat,ios ...

  2. 三 APPIUM GUI讲解(Windows版)

    本文本转自:http://www.cnblogs.com/sundalian/p/5629386.html APPIUM GUI讲解(Windows版)   Windows版本的APPIUM GUI有 ...

  3. selenium初识(一)

    Selenium是一个开源的便携式的自动化软件测试工具,用于测试web应用程序.有能力在不同浏览器和操作系统运行.它是一套工具,帮助我们有效地给予web应用程序的自动化. Selenium分为以下几个 ...

  4. 使用jquery validate结合zui作表单验证

    1.引入jquery validate和zui <!-- jQuery (ZUI中的Javascript组件依赖于jQuery) --> <script src="${_b ...

  5. (原)Unreal渲染模块 管线 - 程序和场景查询

    @author: 白袍小道 查看随意,转载随缘     第一部分: 这里主要关心加速算法,和该阶段相关的UE模块的结构和组件的处理. What-HOW-Why-HOW-What(嘿嘿,老规矩) 1.渲 ...

  6. Thread 线程池

    Thread 线程池: 当使用多个较短存活期的线程有利时,运用线程池技术可以发挥作用.运用这一技术时,不是为每个任务创建一个全新的线程,而可以从线程池中抽出线程,并分配给任务.当线程完成任务后,再把它 ...

  7. 浅谈数据库系统中的cache(转)

    http://www.cnblogs.com/benshan/archive/2013/05/26/3099719.html 浅谈数据库系统中的cache(转)   Cache和Buffer是两个不同 ...

  8. EXTJS4.0 form 表单提交 后 回调函数 不响应的问题

    在提交表单后,应返回一个 JSON 至少要包含{success:true} 否则,EXT 不知道是否成功,没有响应. {success:true,msg:'成功',Url:'http://www.ba ...

  9. [bzoj] 1043 下落的圆盘 || 圆上的“线段覆盖”

    原题 n个圆盘,求下落后能看到的总周长. 红色即为所求 借鉴于黄学长的博客 对于每下落的一个圆盘,处理他后面的圆盘会挡住哪些区域,然后把一整个圆(2\(/pi\))当做一整个区间,每个被覆盖的部分都可 ...

  10. [poj] 1149 PIGS || 最大流经典题目

    原题 题目大意 给你m个猪圈以及每个猪圈里原来有多少头猪,先后给你n个人,每个人能打开一些猪圈并且他们最多想买Ki头猪,在每一个人买完后能将打开的猪圈中的猪顺意分配在这次打开猪圈里,在下一个人来之前 ...