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

muffin
  • 1,456
  • 4
  • 21
  • 44
  • 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 – muffin Jun 24 '13 at 09:48
  • 1
    I muffin he means, "How do I save the entire contents of a text file into a single String?". – Paul Jun 24 '13 at 09:48
  • http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file – rags Jun 24 '13 at 09:48
  • see http://stackoverflow.com/questions/3402735/what-is-simplest-way-to-read-a-file-into-string-in-java or http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file – marsze Jun 24 '13 at 09:48

4 Answers4

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

}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
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);
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 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 Jun 24 '13 at 10:06
  • @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 :-) – Juned Ahsan Jun 24 '13 at 10:09
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

fge
  • 119,121
  • 33
  • 254
  • 329
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();
}
Java Devil
  • 10,629
  • 7
  • 33
  • 48