Java Write files

Source: Internet
Author: User
/* WriteFile.java
Receive keyboard input and output to screen as-is
In addition, the data entered by the keyboard is stored sequentially in the file
How to use: Java WriteFile file name
To end this program, enter a # number at the beginning of the row.
*/

Import java.io.*;

Class WriteFile
public class WriteFile
{
public static void Main (string[] args)
{
byte[] buff = new byte[1024];
Boolean cont = TRUE; loop control variable
FileOutputStream outfile = null; File Output objects

Generate object outfile, prepare to output to file
Try
{
outfile = new FileOutputStream (args[0]);
}
catch (FileNotFoundException e)
{
SYSTEM.ERR.PRINTLN ("File does not exist");
System.exit (1);
}

The following loop is performed when the beginning of the line does not enter a period
while (cont)
{
Try
{
int n = System.in.read (buff); Reading data from system.in
System.out.write (buff, 0, N); Write to System.out
if (Buff[0]==´#´)
{
Cont = FALSE;
}
Else
{
Outfile.write (buff, 0, N);
}

}
catch (Exception e)
{
System.exit (1);
}
}

Close File
Try
{
Outfile.close ();
}
catch (IOException E)
{
SYSTEM.ERR.PRINTLN ("File Error");
System.exit (1);
}
}
}

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.