在java中从文本文件中删除行号

在java中从文本文件中删除行号,java,Java,写入的代码不会删除所需的行号(即三个数值,例如001、002…999),而是删除除第一行以外的整个文档(显示第一行中不包括000的第一行)。需要删除整个文本文件的第一列,即表格000、001002、003等。 下面是我试图执行的代码 import java.io.*; import java.io.FileNotFoundException; import java.util.Scanner; public class delete{ public static void main(Stri

写入的代码不会删除所需的行号(即三个数值,例如001、002…999),而是删除除第一行以外的整个文档(显示第一行中不包括000的第一行)。需要删除整个文本文件的第一列,即表格000、001002、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();
    }
  }
} 
import java.io.*;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共类删除{
公共静态void main(字符串[]args){
试试{
File File=new File(“abc.txt”);//创建要读取的文件对象
Scanner Scanner=新建扫描仪(文件);//创建要读取的扫描仪
while(scanner.hasNextLine()){//当有下一行时
//hasNextLine()&&
String line=scanner.nextLine();//line=下一行
char[]ch=line.toCharArray();
StringBuilder sb=新的StringBuilder(线);
//替换字符
字符串换行=”;
对于(int i=0;i
您正在for循环中调用scanner.nextLine(),这样它就可以在整个文档中循环,而不会向打印编写器添加任何内容。文档只有第一行的原因是,您不断地将换行符猛击为sb.toString(),这与您在开始时声明换行符时从未改变


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

您已经编辑了您的问题,现在问题更清楚了,谢谢。首先,您有3个循环,它们都做相同的事情。我简化了代码,缩进了4个空格(不是2个),并缩短了代码,就像这样。逐行描述包括:

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

这并不能真正回答问题。您针对一个问题(有很多问题),但您没有尝试解决它。对于我的前两个答案,我感到抱歉。你的问题以前不清楚,但现在清楚了。非常感谢。我在下面贴了一个正确的答案。