在数据集中找到一行并复制到 java 中的另一个文件

Find a line in a dataset and copy to another file in java

我正在寻找一个小代码片段,它可以找到第一列中有多少个特定数字的单元格,并将 1/4 的行(所有行)剪切到另一个文件。因此,例如我在以下文件中:

dataset.txt:

0 139 0.22 0.34 0.48 0.50 0.42 0.29 0.39

0 140 0.44 0.35 0.48 0.50 0.44 0.52 0.59

0 141 0.27 0.42 0.48 0.50 0.37 0.38 0.43

0 142 0.16 0.43 0.48 0.50 0.54 0.27 0.37

1 143 0.06 0.61 0.48 0.50 0.49 0.92 0.37

代码会在第一列找到多少 0 并将 1/4 行带到另一个文件,我得到这样的文件:

myFile.txt:

0 139 0.22 0.34 0.48 0.50 0.42 0.29 0.39

我的代码:

public static void main(String[] args) {
    String in = "File.txt";
    String out = "File_new.txt";
    convert(in, out);
}

private static void convert(String inPath, String outPath) {
    try {
        BufferedReader reader = new BufferedReader(new FileReader(inPath));
        BufferedWriter writer = new BufferedWriter(new FileWriter(outPath));
        String line;
        while((line = reader.readLine()) != null) {

         String[] aaa = line.split(" ");
         String newLine = "";                                  
         if(aaa[0].equals("0"))
         for(int i =0; i < aaa.length; i++)
         newLine += aaa[i]+' ';
         newLine += '\n';                                       
         writer.write(newLine);  
            // ?

        }
        reader.close();
        writer.close();
    } catch(Exception exc) {
        System.out.print(exc.getMessage());
    }
}

while里面的实现应该是什么样子的? 我做了这个:这个函数将行写入从 0 开始的新文件行,现在我如何在复制到新文件后删除这一行?

首先,如果我正确理解你的问题,你只需要在第一个字符是 0 时执行代码。我会做类似的事情:

while(has next character) {
     while (first character is != 0) {
         writer.print(...);
         ...;
     }
}

这只是您要完成的伪代码。请尝试自己编写,如果不能,请进行编辑。希望对您有所帮助!

编辑**

下面列出了执行此操作的代码:

while(reader.hasNext()) {
     while (reader.nextDouble() == 0.0) {
         writer.print(reader.nextLine());
     }
}

不确定我是否理解你的问题,但我会做以下事情:

    //1. Based on my file path
    String filePath = "c:/mydatafile.txt";
    //2. I will reference the file path using "File" class
    File myTxtFile = new File(filePath);
    //3. Then BufferedReader to read the file
    BufferedReader bufferedReader = new BufferedReader(new FileReader(myTxtFile));
    //4. Start reading the file
    //You would like to have a variable to evaluate each line in your file
    String line = null;
    //StringBuilder to take the characters I want and save them to this
    StringBuilder appender = new StringBuilder();
    //Then loop...
    while ((line = bufferedReader.readLine()) != null) {
        //Evaluate the current line being read by the bufferedReader
        //You can use whatever you feel comfortable here: regex (matches), tokenizer, startsWith... but you need to think this
        if (condition) {
            appender.append(myVal);
        }
    }

    //Close the reader
    bufferedReader.close();

    //At the end I'll have my stringbuilder "appender" with the information I want and the only thing I would do is:
    String targetFile = "c:/mynewfile.txt";
    File myNewFile = new File(targetFile);

    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(myNewFile));
    bufferedWriter.write(appender.toString());
    //Close the writer
    bufferedWriter.close();

也许这对您要查找的内容有所帮助。编码愉快!

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

     String[] aaa = line.split(" ");
     String newLine = "";                                  
     if(aaa[0].equals("0"))
     for(int i =0; i < aaa.length; i++)
     newLine += aaa[i]+' ';
     newLine += '\n';                                       
     writer.write(newLine);  


    }