如何通过知道其位置从文件中删除一行?

rym*_*rym 0 java file-io

我想从我的文件中删除一行(特别是第二行),所以我使用了另一个文件进行复制,但使用下面的代码,第二个文件包含完全相同的文本.(我的原始文件.txt和我的最终文件.XML)

public static File fileparse() throws SQLException, FileNotFoundException, IOException {  
    File f=fillfile();//my original file
    dostemp = new DataOutputStream(new FileOutputStream(filetemp));
    int lineremove=1;
    while (f.length()!=0) {
        if (lineremove<2) {
            read = in.readLine();
            dostemp.writeBytes(read);     
            lineremove++;
        }

        if (lineremove==2) {
            lineremove++;
        }

        if (lineremove>2) {
            read = in.readLine();
            dostemp.writeBytes(read); 
        }
    }

    return filetemp;
}
Run Code Online (Sandbox Code Playgroud)

mor*_*rja 5

如果lineremove为2,则不读取该行,并且在它为2时增加它时检查它是否大于2.请执行以下操作:

int line = 1;
String read = null;
while((read = in.readLine()) != null){
   if(line!=2)
   {
     dostemp.writeBytes(read);     
   } 
   line++;
}
Run Code Online (Sandbox Code Playgroud)