如何让 Java 读取文件中的所有文本?

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

我正在尝试让 Java 从文件中读取文本,以便将文本转换为一系列 ascii 值,但目前它似乎只读取和检索文件的第一行txt文件。我知道这是因为输出比文件中的文本短得多。

文件中的文本如下:

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

我认为因为每行文本之间都有一个空行,所以程序只读取第一行然后在看到后面没有行时停止,但实际上是向下 2 行。

这是我写的代码:

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");
        }
    }
}

我在代码中对我认为有问题的地方进行了评论。第一个 while 条件使用 hasNextLine() 检查是否有下一行,我尝试使用方法 ReadAllLines() 但它说此方法未定义类型扫描器。

如何让程序读取和检索整个文本文件而不是第一行?

谢谢!

读取整个输入流:

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

然后只是:

String entireInput = sc.next();

这是通过将标记定界符设置为所有输入的开始来实现的,当然在读取任何字节后都不会遇到这种情况,因此“下一个”标记是整个输入。

对于每次执行,您都会检查硬编码文件名是否已创建或已经存在。如果它已经存在,你碰巧打破了停止执行的循环。 https://www.javatpoint.com/java-break