For an interview, see Java I/O stream. Example is a lot to see, but what the master how, do not know. Tired of watching, I wrote this small program.
is to chat in console A with B, and then the Chat class capacity branch to write the file.
Import java.io.*;
Import Java.util.Scanner;
public class self{public
static void Main (string[] args) {
Scanner scanner=new Scanner (system.in);
String self= "";
int i=0;
Try{file file=new File ("D:/self.txt"); Creates a Self.txt file while
(true) {
i++;
if (i%2==1) {
System.out.println ("A:");
OutputStream out=new FileOutputStream (file,true); Creates an output stream to file, calls this construct method, can write the contents of the console to the file
self=scanner.nextline () each time; Read the contents of the scanner, you must call the Nextline () method to read the one-time input, next () only recognize a word
String self1= "A:" +self+ "\ r" + "\ n"; The back is for the branch
byte[] Bytes=self1.getbytes ();
Out.write (bytes);
Out.close ();
} else{
System.out.println ("B:");
OutputStream out=new FileOutputStream (file,true);
Self=scanner.nextline ();
String self2= "B:" +self+ "\ r" + "\ n";
Byte[] B=self2.getbytes ();
Out.write (b);
Out.close ();}}
catch (Exception e) {
e.printstacktrace ();}}}
This is the effect chart