简体   繁体   中英

How to store strings of text files for later use in Java

Please forgive me, I'm relatively new to Java.

Basically I'm wanting to get some string for a text file for later use after selecting the file through a filechooser.

This is a snippet of the code that I have written so far.

public void actionPerformed(ActionEvent e){
    if(e.getSource() == openButton){
        returnVal = fileChooser.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION){
            file = fileChooser.getSelectedFile();

            //read file
            try{
                br = new BufferedReader(new FileReader(file));
                while((currentLine = br.readLine()) != null){
                    if(currentLine.startsWith(organismId)){
                   // if(Character.isDigit(currentLine.charAt(7))){
                    System.out.println(currentLine);
                }}
            } catch (Exception error){
                error.printStackTrace();
            }
        }
    }
}

Basically I have a variable (organismId) which is determined by user input through a GUI. From this I can print out the lines which begin with the chosen String variable. However this isn't exactly what I want to achieve. I want to be able to gather the text that is present on the next line, until a character ">" is reached.

I then want to be able to get an average character length for the text for each different organismId (ie >ggo and >hba).

Ie an example of what the text file may look like is as follows:

>ggo

This is an example sentence

>ggo

This is another example sentence

>ggo

This is a further example sentence which is extremely long and can go onto more than one line as is demonstrated here

>hba

This is a sentence with a different organismId

I hope that all of this makes sense and any help would be much appreciated.

Many Thanks! :)

This code snippet should be a solution to your problem. printLines is used to start printing lines after your organism has been found. The while loop continues round and if another organism is not found, then the line is printed. But when a new organism is reached (denoted by > I believe), then printLines is set back to false .

ArrayList<String> organisms = new ArrayList<String>();
boolean printLines = false;
StringBuilder organism = new StringBuilder();
while((currentLine = br.readLine()) != null) {
    if (printLines) {
        if (currentLine.startsWith(">")) {
            // We have reached the next organism, so stop printing
            printLines = false;
            // Add the current organism to our collection
            organisms.add(organism.toString());
            // Clear the StringBuilder, ready for the next organism
            organism.setLength(0)
        }
        else
        {
            // We are still printing the current organism
            organism.append(currentLine);
        }
    }

    if(currentLine.startsWith(organismId)) {
        // Print this line, and start printing all lines after this
        organism.append(currentLine);
        printLines = true;
    }
}

Let me know below if you have any queries or comments.

Edit As requested in the comments, have edited to add values into an ArrayList .

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