Skip to content
Advertisement

How can I get Java to read all text in file?

I am trying to get Java to read text from a file so that I can convert the text into a series of ascii values, but currently it only seems to be reading and retrieving the first line of the txt file. I know this because the output is much shorter than the text in the file.

The text in the file is below:

AD Mullin Sep 2014 https://hellopoetry.com/poem/872466/prime/

Prime

Have you ever thought deeply about Prime numbers?

We normally think of prime as something unbreachable

In base ten this is most likely true

But there are other languages that might be used to break down numbers

I'm no theorist but I have my theories

What was behind the Big Bang?

Prime

If impermeable ... then the Big Bang never happened

And any good programmer worth a lick of salt, always leaves a back door

So, I bet there are some Prime numbers out there that are permeable, otherwise ...

We wouldn't be the Children of the Big Bang

I think because each line of text has an empty line between them the program is only reading the first line then stopping when it sees there is no line after it, but in facts 2 lines down instead.

Here is the code I have written:

package poetry;

import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;

public class poetry {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //Below try catch block reads file text and encodes it.
        try {
            File x = new File("/Users/jordanbendon/Desktop/poem.txt");
            Scanner sc = new Scanner(x);
            
            //Right below is where I think the issue lies!

            while(sc.hasNextLine()) {
                String lines = sc.nextLine();
                
                char[] stringArray = lines.toCharArray();
                
                String result = "";
                
                for (int i = 0; i < lines.length(); i++) {
                    int ascii = lines.codePointAt(i);
                    if ((ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122)) {
                        ascii += 15;
                        result += Integer.toString(ascii);
                    } else {
                        result += stringArray[i];
                    }
                }
                System.out.println(result);
                
                
                //Try catch block here creates a new file.
                
                try {
                      File myObj = new File("/Users/jordanbendon/Desktop/EncryptedMessage.txt");
                      File s = myObj;
                      
                      if (myObj.createNewFile()) {
                        System.out.println("File created: " + myObj.getName());
                      } else {
                        System.out.println("File already exists.");
                        break;
                      }
                    } catch (IOException e) {
                      System.out.println("An error occurred.");
                      e.printStackTrace();
                    }
                
                //Try catch block here writes the new encrypted code to the newly created file. 
                
                try {
                      FileWriter myWriter = new FileWriter("/Users/jordanbendon/Desktop/EncryptedMessage.txt");
                     
                    myWriter.write(result);
                      myWriter.close();
                    } catch (IOException e) {
                      System.out.println("An error occurred.");
                      e.printStackTrace();
                    }
                
                
            }}
        
        catch(FileNotFoundException e) {
            System.out.println("error");
        }
    }
}

I have commented in the code where I think the issue is. The first while condition checks whether there is a next line by using the hasNextLine(), I have tried using the method ReadAllLines() but it says this method is undefined for the type scanner.

How can I get the program to read and retrieve the entire text file instead of the first line?

Thanks!

Advertisement

Answer

To read the entire input stream:

Scanner sc = new Scanner(x).useDelimiter("\A");

then just:

String entireInput = sc.next();

This works by setting the token delimiter to start of all input, which of course is never encountered after any byte read, so the “next” token is the entire input.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement