从java

时间:2017-06-11 21:34:37

标签: java

编写的代码不会删除所需的行号(即三个数值,例如001,002 .... 999),而是删除整个文档,第一行除外。(显示第一行)从第一行排除000)。需要删除整个文本文件的第一列,即000,001,002,003等形式。 这是我试图执行的代码。

import java.io.*;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class delete{

public static void main(String[] args){

try{ 

 File file = new File("abc.txt");  // create File object to read from
 Scanner scanner = new Scanner(file);       // create scanner to read

 while(scanner.hasNextLine()){  // while there is a next line

 // hasNextLine() && 

     String line = scanner.nextLine();  // line = that next line
     char []ch = line.toCharArray();
     StringBuilder sb = new StringBuilder(line);

     // replace a character
     String newLine = ""; 
     for(int i = 0; i < line.length();i++){

             if((scanner.hasNextLine()) && (Character.isDigit(ch[i]))){
               // delete three characters and go to next line 
               char chr = ' ';
               sb.setCharAt(0,chr);                  
               sb.setCharAt(1,chr);
               sb.setCharAt(2,chr);
               scanner.nextLine();
               newLine = sb.toString();

           } 

        }

      // print to another file.
      PrintWriter writer = new PrintWriter("abcd.txt"); // create file to write to
      writer.println(newLine);
      writer.flush();
      writer.close();
      scanner.close();
         }
     }catch (Exception ex){
    ex.printStackTrace();
    }
  }
} 

2 个答案:

答案 0 :(得分:0)

您正在for循环中调用scanner.nextLine(),因此它循环遍历整个文档,而不会向打印编写器添加任何内容。文档只有第一行的原因是你不断地将newLine关闭为sb.toString(),它在你开始声明它时永远不会改变。

你根本不需要内部for循环,因为外部while循环遍历每一行。只需遍历这些行并检查第一个字符是否为数字,如果是则替换。

答案 1 :(得分:0)

You have edited your question, and now it is more clear, thank you. To start, you have 3 loops that all do the same thing. I have simplified, indented 4 spaces(not 2), and shortened your code, like so. Line by line descriptions included:

public static void main(String[] args) {
    try { 
        File file = new File("abc.txt"); //change to the location of your file
        PrintWriter writer = new PrintWriter("abcd.txt"); //change to where you would like your file to be
        Scanner scanner = new Scanner(file);

        while(scanner.hasNextLine()) {  //if statement and for loop are useless
            String line = scanner.nextLine(); //increments line
            StringBuilder sb = new StringBuilder(line); //makes line a sequence of char's
            String newLine; //used for printing to file
            char chr = ' '; //creates char equal to a space
            sb.setCharAt(0, chr); //replaces first,           
            sb.setCharAt(1,chr); //second,
            sb.setCharAt(2,chr); // and third char's with ' '
            newLine = sb.toString();
            writer.println(newLine); //prints the line without numers 
            writer.flush(); //flushes the stream
        }
    } catch (Exception ex) { //in case the file is not found
        ex.printStackTrace(); //prints the error
    }//end catch
}//end main