Firstly we shall cover reading a file
.:READING:.
import java.io.*;
public class File {
BufferedReader in;
String read;
public File(){
try {
//open a bufferedReader to file helloworld.txt
in = new BufferedReader(new FileReader("helloworld.txt"));
//read a line from helloworld.txt and save into a string
read = in.readLine();
//print out the line
System.out.println("file output: " + read);
//safely close the BufferedReader after use
in.close();
}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 to always put it inside a try/catch block that catches an IOException; you'll find this will make it easier as it will output useful error information.
Replace helloworld.txt with the file name you want to read.
This is all good, but what if you want to read a specific line in a File that contains multiple lines? Well it's just as easy.
Let's say 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 it's done
import java.io.*;
public class File {
BufferedReader in;
String read;
//the specific line i want to read
int linenum = 3;
public File(){
try {
in = new BufferedReader(new FileReader("helloworld.txt"));
while(linenum > 0){
//read the next line until the specific line is found
read = in.readLine();
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 that's all for Reading from a file.
.:WRITING TO A FILE:.
Writing to a file in Java is just as easy. This is how it's done
import java.io.*;
public class File {
BufferedWriter out;
String read;
int linenum = 3;//the specific line i want to read
public File(){
try {
//replace helloworld.txt with the name of the file
out = new BufferedWriter(new FileWriter("helloworld.txt"));
//Write out the specified string to the file
out.write("Hello 83743");
//flushes and closes the stream
out.close();
}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 were writing to, and you will see it will say what you wrote out; in my case Hello 83413. Wait, there seems to be a problem. What happened 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.
import java.io.*;
public class File {
BufferedWriter out;
public File(){
try {
//replace helloworld.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 = new BufferedWriter(new FileWriter("helloworld.txt",true));
//Write out a string to the file
out.write("Hello you stupid duck");
//write a new line to the file so the next time you write
//to the file it does it on the next line
out.newLine();
//flushes and closes the stream
out.close();
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args){
File File = new File();
}
}
There. Now 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 it's a bit shabby it's my first time.
This post has been edited by JackOfAllTrades: 01 November 2010 - 03:08 PM







MultiQuote








|