Write each input of the console to a file and branch

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.