just follow following instructions--
For writting to a file:
File f=new File("file.txt");
FileWriter fw=new FileWriter(f);//creating file for writting
fw.write("sample text");//writing data to a file
fw.flush();
fw.close();
For reading a file:
FileReader fr=new FileReader(f); //f is a object of File
char[] in=new char[20];//to read characters
int length=fr.read(in);//will read written data to file
fr.close();
don't forget to put whole i/0 code in try catch block....
then it will work surely....
import java.io.*;
public void read()throws IOExcpetion{
BufferedReader br=new BufferedReader(new FileReader("c:\text.txt"));
String s="";
while (br.ready()){
s+=br.readLine() + "\n";
}
br.close();
System.out.println(s);
}
public void write(String content)throws IOException{
BufferedWriter bw=new BufferedWriter(new FileWriter("c:\text1.txt"));
bw.write("hiiiiiiiiiiiiiiii");
bw.write("how ru ");
bw.flush();
bw.close();
}
hope it might help
Try this code. The comments are in swedish so if your from sweden you understand them to.. Otherwise just use the code and try it out.
import java.util.*;
import java.io.*;
import static java.lang.System.out;
class Demo27
{
public static void main (String [] args) throws IOException
{
//Stringfil = "fil.txt";//g錼 att skriva vart man vill att filen ska sparas. t.ex.
// "c:\\demos\\fil.txt"
//
// "/." + File.separatorChar + demos + File.separatorChar + "fil.txt";
// "/." = root. "." = nuvarande katalog.
// File = new File (fil);
File fil = new File ("fil.txt");
try
{
//FileWriterfw = new FileWriter (fil); //skriv (fil, true) om du vill forts鋞ta skriva i filen
OutputStreamWriter osw = new OutputStreamWriter (System.out, "Cp850");
PrintWriterfout = new PrintWriter (osw, true);
char c = 'A';
char[]v = {'a','?,'?,'?,'e'};
Strings = "Morgonen";
Stringrs = System.getProperty ("line.separator"); // skapar en ny rad
fout.write (c);
fout.write (rs);
fout.write (v);
fout.write (rs);
fout.write (s);
fout.write (rs);
out.println ("GGGGGGGG");
fout.close ();
InputStreamReaderisr =
new InputStreamReader (System.in);
BufferedReaderfin = new BufferedReader (isr);
Stringrad = fin.readLine ();
while (!rad.equals (""))
{
out.println (rad);
rad = fin.readLine ();
}
//StringBuilder sb = new StringBuilder ();
/*intk = fin.read ();
while (k != -1)
{
chart = (char) k;
sb.append (t);
k = fin.read ();
} */
/*Scannerfin = new Scanner (fil);
while (fin.hasNextLine ())
{
Stringrad = fin.nextLine ();
out.println (rad);
}*/
//String text = sb.toString ();
//out.println (text);
fin.close ();
}
catch (IOException e)
{
out.println (e);
}
}
}