muffin
muffin

Reputation: 1486

Save content of File as String in Java?

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

Upvotes: 1

Views: 1797

Answers (4)

Java Devil
Java Devil

Reputation: 10959

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

Upvotes: 1

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35577

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

}

Upvotes: 2

fge
fge

Reputation: 121760

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

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

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

Upvotes: 1

Related Questions