Java file Read and write Example 1

Source: Internet
Author: User
Tags readline

StringBuffer commonly used for string append, insert, delete

Import java.io.*;

public class Iofile {
Static file F=new file ("Test.txt");
static int count=0;
public static void Main (string[] args)
{
if (!f.exists ())
{
Try
{
F.createnewfile ();
}catch (IOException E1)
{
SYSTEM.OUT.PRINTLN (E1);
}catch (Exception E2) {
SYSTEM.OUT.PRINTLN (E2);
}
}
for (int i=0;i<5;i++)
{
Try
{
FileWriter fos=new FileWriter (f,true);
Fos.write (string.valueof (i));
Fos.write (' \ n ');
Fos.flush ();
Fos.close ();
}catch (FileNotFoundException e)
{
System.out.println (e);
}catch (IOException E1)
{
SYSTEM.OUT.PRINTLN (E1);
}
}
Try
{
BufferedReader br=new BufferedReader (New FileReader ("test.txt"));
String Str=null;
For (Str=br.readline (); Str!=null;str=br.readline ())
System.out.println (str);
Br.close ();

}catch (FileNotFoundException E1)
{
SYSTEM.OUT.PRINTLN (E1);
}catch (IOException E2)
{
SYSTEM.OUT.PRINTLN (E2);
}

}

}

Java file Read and write Example 1

Related Article

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.