1

I was working a little bit with config files and file reader classes in java. I always read/wrote in the files with arrays because I was working with objects. This looked a little bit like this:

public void loadUserData(ArrayList<User> arraylist) {
    try {
        List<String> lines = Files.readAllLines(path, Charset.defaultCharset());
        for(String line : lines) {
            String[] userParams = line.split(";");

            String name = userParams[0];
            String number= userParams[1];
            String mail = userParams[2];

            arraylist.add(new User(name, number, mail));
        }   
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This works fine, but how can I save the content of a file as only one single string?

When I read a file, the string I use should be the exact same as the content of the file (without the use of arrays or line splits). how can I do that?

Edit:

I try to read a SQL-Statement out of a file to use it with JDBC later on. That's why I need the content of the File as a single String

4
  • Oh, I'm sorry that I didn't tell above. I try to read a SQL-Statement out of a file to use it with JDBC later on. That's why I need the content of the File as a single String Commented Jun 24, 2013 at 9:48
  • 1
    I muffin he means, "How do I save the entire contents of a text file into a single String?". Commented Jun 24, 2013 at 9:48
  • stackoverflow.com/questions/326390/… Commented Jun 24, 2013 at 9:48
  • see stackoverflow.com/questions/3402735/… or stackoverflow.com/questions/326390/… Commented Jun 24, 2013 at 9:48

4 Answers 4

2

This method will work

public static void readFromFile() throws Exception{
        FileReader fIn = new FileReader("D:\\Test.txt");
        BufferedReader br = new BufferedReader(fIn);
        String line = null;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }
        String text = sb.toString();
        System.out.println(text);

}
Sign up to request clarification or add additional context in comments.

Comments

1

I hope this is what you need:

public void loadUserData(ArrayList<User> arraylist) {
    StringBuilder sb = new StringBuilder();
    try {
        List<String> lines = Files.readAllLines(path, Charset.defaultCharset());
        for(String line : lines) {
           // String[] userParams = line.split(";");

            //String name = userParams[0];
            //String number= userParams[1];
            //String mail = userParams[2];
            sb.append(line);
        }   
        String jdbcString = sb.toString();
        System.out.println("JDBC statements read from file: " + jdbcString );
    } catch (IOException e) {
        e.printStackTrace();
    }
}

or maybe this:

String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
System.out.println(content);

2 Comments

Don't you need to add a space (or something) between the lines? The javadoc for readAllLines doesn't specify whether it retains or discards the line ends. If it discards them - which is consistent with the way LineNumberReader, and other Java methods, treat them - you will want to add a space after each appended line, I think.
@Paul As he mentioned the file contains sql scripts so i assumed them to be delimited by ';'. Also the idea is to help to move things but not to do the entire things for the questioner. I think he is able to move :-)
1

Just do that:

final FileChannel fc;
final String theFullStuff;

try (
    fc = FileChannel.open(path, StandardOpenOptions.READ);
) {
    final ByteBuffer buf = ByteBuffer.allocate(fc.size());
    fc.read(buf);
    theFullStuff = new String(buf.array(), theCharset);
}

nio for the win! :p

Comments

1

You could always create a Buffered reader e.g.

File anInputFile = new File(/*input path*/);
FileReader aFileReader = new FileReader(anInputFile);
BufferedReader reader = new BufferedReader(aFileReader)

String yourSingleString = "";
String aLine = reader.readLine();

while(aLine != null)
{
    singleString += aLine + " ";
    aLine = reader.readLine();
}

2 Comments

Not bad, but the answer of Juned Ahsan is even better... Thanks for your help anyway :)
Prefer to use a StringBuilder. Then use its toString() method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.