There is a very basic way in writing and reading to a file in java using the java.io pakage.
Firstly we shell cover reading a file
.:READING:.
CODE
import java.io.*;
public class File {
BufferedReader in;
String read;
public File(){
try {
in = new BufferedReader(new FileReader("helloworld.txt"));//open a bufferedReader to file hellowrold.txt
read = in.readLine();//read a line from helloworld.txt and save into a string
System.out.println("file output: " + read);//print out the line
in.close();//safley close the BufferedReader after use
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args){
File File = new File();
}
}
Easy as that.Remember when handling a BufferedReader always put it inside a try method that catches an IOException , you'll find this will make it easier as it will output useful error information.
replace helloworld.txt with the filename you want to read.
This is all good but what if you want to read a specific line in a File that containts multiple lines.
well its just as easy lets says helloworld.txt contains this:
hello world
hello keyboard
hello mouse
hello test
and i want to read line 3 which is "hello mouse":
this is how its done
CODE
import java.io.*;
public class File {
BufferedReader in;
String read;
int linenum = 3;//the specfic line i want to read
public File(){
try {
in = new BufferedReader(new FileReader("helloworld.txt"));
while(linenum > 0){
read = in.readLine();//read the next line until the specfic line is found
linenum--;
}
System.out.println("file output: " + read);
in.close();
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args){
File File = new File();
}
}
Easy isn't it replace the integer for linenum to the specific line you which to read.
And thats all for Reading from a file.
.:WRITING TO A FILE:.
Writing to a file in java is just as easy this is how its done
CODE
import java.io.*;
public class File {
BufferedWriter out;
String read;
int linenum = 3;//the specfic line i want to read
public File(){
try {
out = new BufferedWriter(new FileWriter("helloworld.txt"));//replace hellowrold.txt with the name of the file
out.write("Hello 83743");//Write out the specfied string to the file
out.close();//flushes and closes the stream
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args){
File File = new File();
}
}
Yep that easy,run this then open the file you where writting to,as you will see it will say what you wrote out in my case Hello 83413,wait there seems to be a problem what happend to the rest of the lines:0,well there is an easy fix for that:)
This is how you can write to a file without erasing its content.
CODE
import java.io.*;
public class File {
BufferedWriter out;
public File(){
try {
out = new BufferedWriter(new FileWriter("helloworld.txt",true));//replace hellowrold.txt with the name of the file CHANGE after
//you declare which file to write to add the boolean true which will stop it from replacing the helloworld.txt with a new one.
out.write("Hello you stupid duck");//Write out a string to the file
out.newLine();//write a new line to the file so the next time you write to the file it does it on the next line
out.close();//flushes and closes the stream
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args){
File File = new File();
}
}
There know every time you write to the file it will keep its contents and write to the next line:)
Thanks for reading my tutorial sorry if its a bit shabby its my first time.