-1

(The file includes a paragraph)

A rainy day is the bearer of good weather with refrigerating breeze and rain showers. It refreshes everyone by making the climate cool and delightsome and brings in a sigh of relief from the scorching heat. Rainy day gives us relief from the usually hot and humid climate.

Can I just save the entire paragraph to a single String variable (say its called "fullStory")?

File story = new File("story.txt");
//Help read words in file
FileInputStream fileStr1 = new FileInputStream(story);
//Reads one character
InputStreamReader input_1 = new InputStreamReader(fileStr1);
//BufferedReader reads from the input stream
BufferedReader wordReader1 = new BufferedReader(input_1);
Chaos
  • 1
  • 1
  • Sure why not? But consider using a `StringBuilder` – Scary Wombat Jun 09 '21 at 01:31
  • Can you elaborate a little bit? @ScaryWombat – Chaos Jun 09 '21 at 01:34
  • 1
    I just answered your question in full. What elaboration are you after? Maybe **you** should elaborate on your question? What have you do so far and what problems are you facing? – Scary Wombat Jun 09 '21 at 01:35
  • Basically I have a paragraph saved in a text file. I want to save it as a string variable so i can alter it by traversing through each character and then print the altered version. I'm trying to find the code online I need to convert all the contents of the text file to a single string. @ScaryWombat – Chaos Jun 09 '21 at 01:38
  • Well show us your code so that we can see what is wrong with it. – Scary Wombat Jun 09 '21 at 01:40
  • Just updated (it's all I have so far) @ScaryWombat – Chaos Jun 09 '21 at 01:47

1 Answers1

-1

This here reads all bytes of a file:

static public byte[] readBytes(final File pFile) throws FileNotFoundException, IOException {
    final long size = pFile.length();
    if (size > Integer.MAX_VALUE) throw new RuntimeException("File size is too big to be read into array!");

    final byte[] ret = new byte[(int) size];
    try (FileInputStream fis = new FileInputStream(pFile)) {
        fis.read(ret);
    }

    return ret;
}

So once you get the bytes, you can convert it into a String:

byte[] data = readBytes(file);
String text = new String(text, StandardCharsets.UTF_8);

What I need to mention: this can only read files up to 2 GBytes, because 2^31-1 is the max amount of bytes an int can address and thus a Java array can hold data.

But having one text file with such a size is highly unlikely and by design a problem, so this here is the fastest and easiest way.

Since Java 1.7 there's also Files.readAllBytes(path), but that hits the same restrictions.

JayC667
  • 2,418
  • 2
  • 17
  • 31
  • Thank you very much but would something simpler like this work? String content = new String(Files.readAllBytes(Paths.get("story.txt"))); @JayC667 – Chaos Jun 09 '21 at 02:01
  • Depends on your Java version. 1.7+, your method works. Just be aware of the Encoding when you create the String, as in my example I explicitly use `StandardCharsets.UTF_8` to make it clear the file might have another encoding. – JayC667 Jun 09 '21 at 09:35