简体   繁体   English

Java读取单个文件并写出多个文件

[英]Java read in single file and write out multiple files

I want to write a simple java program to read in a text file and then write out a new file whenever a blank line is detected. 我想编写一个简单的java程序来读取文本文件,然后每当检测到空行时写出一个新文件。 I have seen examples for reading in files but I don't know how to detect the blank line and output multiple text files. 我看过文件读取的例子,但我不知道如何检测空白行并输出多个文本文件。

fileIn.txt: fileIn.txt:

line1
line2

line3

fileOut1.txt: fileOut1.txt:

line1
line2

fileOut2.txt: fileOut2.txt:

line3

You can detect an empty string to find out if a line is blank or not. 您可以检测空字符串以查明行是否为空。 For example: 例如:

if(str!=null && str.trim().length()==0)

Or you can do (if using JDK 1.6 or later) 或者你可以做(​​如果使用JDK 1.6或更高版本)

if(str!=null && str.isEmpty())

I don't know how to detect the blank line.. 我不知道如何检测空行..

if (line.trim().length==0) { // perform 'new File' behavior

.. and output multiple text files. ..并输出多个文本文件。

Do what is done for a single file, in a loop. 在循环中对单个文件执行的操作。

Just in case your file has special characters , maybe you should specify the encoding . 如果您的文件有特殊字符 ,也许您应该指定编码

FileInputStream inputStream = new FileInputStream(new File("fileIn.txt"));
InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader reader = new BufferedReader(streamReader);
int n = 0;
PrintWriter out = new PrintWriter("fileOut" + ++n + ".txt", "UTF-8");
for (String line;(line = reader.readLine()) != null;) {
    if (line.trim().isEmpty()) {
        out.flush();
        out.close();
        out = new PrintWriter("file" + ++n + ".txt", "UTF-8");
    } else {
        out.println(line);
    }
}
out.flush();
out.close();
reader.close();
streamReader.close();
inputStream.close();
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
String line;
int empty = 0;
while ((line = br.readLine()) != null) {
if (line.trim().isEmpty()) {
 // Line is empty
 }
}

The above code snippet can be used to detect if the line is empty and at that point you can create FileWriter to write to new file. 上面的代码片段可用于检测行是否为空,此时您可以创建FileWriter以写入新文件。

Something like this should do : 这样的事情应该做:

public static void main(String[] args) throws Exception {
        writeToMultipleFiles("src/main/resources/fileIn.txt", "src/main/resources/fileOut.txt");
    }

    private static void writeToMultipleFiles(String fileIn, String fileOut) throws Exception {      

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileIn))));
        String line;
        int counter = 0;
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileOut))));

        while((line=br.readLine())!=null){

            if(line.trim().length()!=0){
                wr.write(line);
                wr.write("\n");
            }else{
                wr.close();
                wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileOut + counter)));
                wr.write(line);
                wr.write("\n");
            }
            counter++;
        }

        wr.close();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM