简体   繁体   中英

Read specific characters from specific line of text in java

I am working on a program that involves me having to search a specific line in a .txt file and convert the string inside of it into something else.

For example, the string is actually made of numbers which I suppose I can convert into ints. The main thing is that for example, on line 2, there are 5 digits for zip code stored. I need to convert that into certain outputs, depending on the numbers. In other words, I need variables from digits 0-9 and depending on each digit, output a specific output.

Right now here is the code I have to prompt the user for information that is stored in the file, and can read and print all of the information that was just typed, but I'm unsure how to go about the rest.

import java.io.*;
import java.util.*;
import java.io.FileReader;
import java.io.IOException;

public class ObjectTest2 {
public static void main(String [] args) throws FileNotFoundException, IOException {

    // The name of the file to open.
    String fileName = "information.txt";
    Scanner myScanner = new Scanner(System.in);

    try {
        // Assume default encoding.
        FileWriter fileWriter =
            new FileWriter(fileName);

        // Always wrap FileWriter in BufferedWriter.
        BufferedWriter bufferedWriter =
            new BufferedWriter(fileWriter);            
        // append a newline character.
        //This shit here prompts the user for information and stores it in seperate lines to be
        //called on by the later section.
        System.out.print("What is your name? ");
        bufferedWriter.write(myScanner.nextLine());
        bufferedWriter.newLine();
        System.out.print("What is your 5 digit zip code?");
        bufferedWriter.write(myScanner.nextLine());
        bufferedWriter.newLine();
        System.out.print("What is your +4 digit zip? ");
        bufferedWriter.write(myScanner.nextLine());
        bufferedWriter.newLine();
        System.out.print("What is your address? ");
        bufferedWriter.write(myScanner.nextLine());

        // Always close files.
        bufferedWriter.close();

        //reads the information file and prints what is typed
        BufferedReader reader = new BufferedReader(new FileReader("information.txt")); {
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                }
                System.out.println(line);
            }
        }           
    }
    catch(IOException ex) {
        System.out.println(
            "Error writing to file '"
            + fileName + "'");
        // Or we could just do this:
        // ex.printStackTrace();
    }
}
}

You are left with no choice but to iterate over each line of the file and search for the String. If you want to get a line of string from the file based on line number, consider creating a method. If the operation is required to be performed several times on the same file and if the contents of the file do not change, use a map to cache the file contents based on the line number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM