Reading, saving input to a text file.

Can someone give me a compilable example of how you could read input from a .txt file? I also need to know how to write data to a .txt file.Thanks in advance!
[172 byte] By [grimakisa] at [2007-11-26 13:21:59]
# 1
http://www.javapractices.com/Topic42.cjp
harishotya at 2007-7-7 17:52:20 > top of Java-index,Java Essentials,Java Programming...
# 2
That example didnt help me very much, im not very experienced in Java. If any one could offer a different method to do this, and possibly explain it.Thanks again!~Grimakis~
grimakisa at 2007-7-7 17:52:20 > top of Java-index,Java Essentials,Java Programming...
# 3
> That example didnt help me very much, im not very> experienced in Java. If any one could offer a> different method to do this, and possibly explain> it.So what is it that is difficult about this standard way to handle reading and writing a text
sabre150a at 2007-7-7 17:52:20 > top of Java-index,Java Essentials,Java Programming...
# 4
Because the instructions didnt offer much explanation.
grimakisa at 2007-7-7 17:52:20 > top of Java-index,Java Essentials,Java Programming...
# 5

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....

siddheshwara at 2007-7-7 17:52:20 > top of Java-index,Java Essentials,Java Programming...
# 6

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

G_Abubakra at 2007-7-7 17:52:20 > top of Java-index,Java Essentials,Java Programming...
# 7
Can someone give me code for a constructor and code for a tester... that I could just pop into BlueJ and see how it works?
grimakisa at 2007-7-7 17:52:20 > top of Java-index,Java Essentials,Java Programming...
# 8

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

}

}

}

Dingo_no_1a at 2007-7-7 17:52:20 > top of Java-index,Java Essentials,Java Programming...