0

I have a problem. I wrote this code that reads a string from a txt file and I exported with the first method a int while the second one particular string. This method is already running but I have used the apache library, now I wanted to rewrite it in Java standard libraries. I have tried this, but I have had problems. Could someone help me? Thank you very much.

package ausiliare;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.*;

public class Read {
public static int getInt() throws IOException {

    String content = null;
    File folder = new File("C:\\Solution.txt");

    content = FileUtils.readFileToString(folder) + "\n";

    int outside = Integer.parseInt(content.substring(0,
            content.indexOf("[")).trim());
    return outside;
}

public static String getString() throws IOException {
    String content = null;
    File folder = new File("C:\\Solution.txt");
    content = FileUtils.readFileToString(folder) + "\n";
    String remainingString = content.substring(content.indexOf(" ["),
            content.lastIndexOf("]") + 1);
    // System.out.println(remainingString);
    return remainingString;

}

public static String[] arg() throws IOException {
    String[] strArray = getString().split(" ");
    // System.out.println(Arrays.toString(strArray));
    return strArray;

}

}

Ps: The input file is txt (for example):

50 [8,24,-22] [-8,34,12] [19,14,47] [-49,32,44] [-41,16,-6] [-49,-11,43]

Where the first method extracts the int 50 and the second extraction method extracts the remaining

theblitz
  • 5
  • 3
  • Welcome to StackOverflow, what have you tried so far? – Tassos Bassoukos Apr 25 '14 at 13:45
  • So, the only method you have used is `FileUtils.readFileToString`, right? Take a look at: http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file – Steffen Apr 25 '14 at 13:53
  • Hi I tried to run, all under standard java library. but I had no problems on the extraction of substrings. I can not get to read the same things I do with apache instead. thanks – theblitz Apr 25 '14 at 13:53

1 Answers1

0
content = new String(Files.readAllBytes(folder.toPath()),
       StandardCharsets.UTF_8);

The missing part is the knowledge of the Files class.

There is a List<String> readAllLines too.

The character set parameter is optional and defaults to the current operating system's encoding - not very portable to other computers.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138