JAVA中IO筛选信息

自学JAVA基础遇到IO的问题,查找了许多大神的帖子尝试写了个筛选信息的日记贴吧,请大家指点。

package dataCheck;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.io.Writer;


/*
 * 筛选出符合要求的日志信息,打印到新的文件中
 */
public class DataFind {
public static void main(String[] args) throws IOException {
String fileName1 = "E:/personal/studyAndTest/a.txt";
String fileName2 = "E:/personal/studyAndTest/b.txt";
提取记录(fileName1, fileName2);
}


/**
* 筛选出符合条件的日志记录

* @param fileName1  目标文件
* @param fileName2  提出数据存储文件
* @throws IOException
*/
public static void 提取记录(String fileName1, String fileName2) throws IOException {
                File myFile1 = new File(fileName1);

               // 原始文件不存在
if (!myFile1.exists()) {
System.out.println("原始文件不存在");
return;
}
String textStyle = 获取编码格式(fileName1);
// 创建新文件
File myFile2 = new File(fileName2);
if (myFile2.exists()) {// 文件存在,删除
myFile2.delete();
System.out.println("文件存在,删除");
}

                //创建写入对象
Writer writer = null;
try {
// 设置编码
writer = new OutputStreamWriter(new FileOutputStream(myFile2),textStyle);
} catch (Exception e) {
e.printStackTrace();
}
                 // 获取总行数
System.out.println(获取文件总行数(myFile1));
String s = null;
int lineNum = 1;// 行数记录
try {
BufferedReader brF = new BufferedReader(new InputStreamReader(
new FileInputStream(myFile1), textStyle));// 注意格式
while ((s = brF.readLine()) != null) {// 按行读
lineNum++;
if (s.contains("添加数据出错")) {
try {
读取文件指定的行数(fileName1, lineNum, fileName2, textStyle,
writer);// 找到对应范围,利用行数进行调整
} catch (IOException e) {
e.printStackTrace();
}
}
}

writer.close();
brF.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
}


// 获取文件的总行数
public static int 获取文件总行数(File file) throws IOException {
FileReader in = new FileReader(file);
LineNumberReader reader = new LineNumberReader(in);
String s = reader.readLine();
int lines = 0;
while (s != null) {
lines++;
s = reader.readLine();
}
reader.close();
in.close();
return lines;
}


// 读取文件指定行内容,写入到指定文件
public static void 读取文件指定的行数(String fileName1, int lineNum,
String fileName2, String textStyle, Writer writer)
throws IOException {
File myFile1 = new File(fileName1);
BufferedReader brF = new BufferedReader(new InputStreamReader(
new FileInputStream(myFile1), textStyle));
String s = null;
if ((lineNum - 3) <= 0 || (lineNum - 3) > 获取文件总行数(new File(fileName1))) {
System.out.println("不存在该行数");
System.exit(0);
}
int lines = 0;
s = brF.readLine();
while (s != null) {
lines++;

                        // 具体位置的行数
if ((lines - lineNum) == -3) {

                                //加个换行,不然打出来一坨
writer.append(s+"\r\n");
}
s = brF.readLine();
}
brF.close();


}


        //详细解释查看我的另一篇转载内容
public static String 获取编码格式(String fileName) throws IOException {

BufferedInputStream bin = new BufferedInputStream(new FileInputStream(
fileName));
int p = (bin.read() << 8) + bin.read();
String code = null;
switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}
return code;
}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值