1

So the problem is I have a text file storing information that has many lines. Each line should look like this:

Product :< name: , Importing Price: , quantity: >

I'm trying to write code that helps the users to input a number and the program will find a line corresponding to that number and remove that line. Any suggestions?

Duong Nguyen
  • 149
  • 4
  • 12
  • Read this: http://stackoverflow.com/questions/20039980/java-replace-line-in-text-file – T D Nguyen Apr 08 '16 at 05:07
  • Read this [How to read a specific line using the specific line number from a file in Java?](http://stackoverflow.com/questions/2312756/how-to-read-a-specific-line-using-the-specific-line-number-from-a-file-in-java) – Braj Apr 08 '16 at 05:20
  • Is it a file that contains fixed length of chars in each line then easily you can jump to specific line number without reading previous ones otherwise you have read all previous lines and keep count on read line numbers. Use [RandomAccessFile](https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html) or [LineNumberReader](https://docs.oracle.com/javase/7/docs/api/java/io/LineNumberReader.html) or [BufferedReader](https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html) – Braj Apr 08 '16 at 05:21

3 Answers3

1

Try to use to below line of code :

FileInputStream fs= new FileInputStream("someFile.txt");

BufferedReader reader = new BufferedReader(new InputStreamReader(fs));
BufferedWriter writer = new BufferedWriter(new FileWriter(fs));

for(int i = 0; i < file_length; ++i){
   br.readLine();
   String lineIWant = br.readLine();
   if(i==your line number){
       i = file_length;
       writer.write(currentLine + System.getProperty("line.separator"));
   }
}
reader.close();
writer.close();
Bhunnu Baba
  • 1,742
  • 15
  • 22
0

You can also use LineNumberReader:

import java.io.LineNumberReader;
import java.io.FileReader;
import java.io.IOException;

class Stackoverflow36491903 {
    public static void main(String[] args) throws IOException {
        int lineNo = Integer.valueOf(args[1]);

        LineNumberReader r = new LineNumberReader(new FileReader(args[0]));
        while (true) {
            String line = r.readLine();
            if (line == null) {
                break;
            }
            if (r.getLineNumber() != lineNo) {
                System.out.println(line);
            }
        }
    }
}

Takes two arguments: name of the file, and a line number.

This just prints the lines removing the line at the requested line number.

Use a writer to write it back to a file.

totoro
  • 2,469
  • 2
  • 19
  • 23
0
Scanner scan = new Scanner(System.in);
int lineNum = scan.nextInt();
File file = new File("file.txt");
FileReader reader = new FileReader(file);
scan = new Scanner(reader);
StringBuilder sb = new StringBuilder("");
while(scan.hasNextLine()) {
    String line = scan.nextLine();
    if (--lineNum != 0) sb.append(line).append(System.getProperty("line.separator"));
}
FileWriter writter = new FileWriter(file);
writter.write(sb.toString());
muzahidbechara
  • 253
  • 1
  • 14