ive tried many different things and this is the only thing that has worked with reading one line from a file so far…
try{
FileInputStream fstream = new FileInputStream("./Saves/Body.sav");
BufferedReader br = new BufferedReader(new InputStreamReader(infstream);
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println(strLine);
w1.Body = strLine;
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
I am trying to create a load function so i can load text from a file onto a string onto a jTextArea… Without any sort of openfiledialog
I’d personally use Guava:
That’s assuming it’s a UTF-8 file, of course. Adjust accordingly.
Your current code has a number of issues:
DataInputStreamfor no obvious reasonBufferedReaderinwhich is in the middle of the chain of inputs for some reason… I’d expect to close eitherbrorfstreaminif there’s no exception (use afinallyblock or a try-with-resources statement if you’re using Java 7)Body, violating Java naming conventionsExceptionrather thanIOException– prefer to catch specific exceptions