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

在数据集中查找一行并复制到java中的另一个文件,java,file,editing,Java,File,Editing,我正在寻找一个小的代码片段,它将在第一列中找到多少个特定数字的单元格,并将1/4行(所有行)剪切到另一个文件中。例如,我在一个文件中有以下内容: //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);

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

    //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();
dataset.txt: 代码将在第一列中找到多少
0
,并将1/4行带到另一个文件,我得到如下文件:

myFile.txt: 我的代码:
publicstaticvoidmain(字符串[]args){
字符串in=“File.txt”;
String out=“File_new.txt”;
转换(输入、输出);
}
私有静态void转换(字符串输入路径、字符串输出路径){
试一试{
BufferedReader=new BufferedReader(new FileReader(inPath));
BufferedWriter writer=新的BufferedWriter(新文件写入程序(输出路径));
弦线;
而((line=reader.readLine())!=null){
字符串[]aaa=line.split(“”);
字符串换行=”;
如果(aaa[0]。等于(“0”))
对于(int i=0;i
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(“”);
字符串换行=”;
如果(aaa[0]。等于(“0”))
对于(int i=0;i
所以不写代码。请告诉我们您的尝试。如果您了解java,只需阅读0或1的行监视,然后复制到新文件,这很容易。如果你不懂java,最好的解决办法就是在网上阅读任何手册。然后复制你的代码,人们会帮助你。现在你有了文件,下一步是计算一行,例如,尝试以下操作:
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();
     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);  


    }