Hanshunping Java Note 第43-46 Speaking IO programming

Source: Internet
Author: User

1. Java stream is divided into two kinds

(1) Byte stream: can be used to read and write binary files and any type of file

(2) Character stream: can be used to read and write text files, cannot manipulate binary files

Stream of Byte stream characters

Input InputStream Reader

Output OutputStream Writer

2. Basic usage of File class

Import java.io.*;

public class io{

Files f1 = new file ("E:\\aa.txt");// Create a Document object

System.out.println ("File path" +f1.getabsolutepath ());//Get the path to the file

SYSTEM.OUT.PRINTLN ("Size of File" +f1.length ());//Get file size, number of bytes

     

File F3 = new file ("E:\\ff");//Create Folder

if (F3.isdirectory ()) {//Determine if the folder exists

System.out.println ("folder exists, cannot be created!") ");

}else{

F3.mkdir ();//Create Folder

}

File F2 = new file ("E:\\ff\\hsp.txt");// Create files and create folders

if (!f2.exists ()) {//Determine if the file exists

try{//can exist

F2.createnewfile ();//Create a new file

}catch (Exception e) {

E.printstacktrace ();

}

}else{

System.out.println ("file exists, cannot be created!") ");

}

File F4 = new file ("E:\\ff"); \ \ list all files under a folder

if (F4.isdirectory ()) {\ \ \ Determines if the folder exists

File lists[] = F4.listfiles ();//Send folder files to lists array

for (int i=0;i<lists.length;i++) {//traversal array

SYSTEM.OUT.PRINTLN ("Show filename is" +lists[i].getname ());//output folder all filenames

}

}

}

3. Reading files

Import java.io.*;

public class io{

public static void Main (string[] args) {

File F = new file ("E:\\ff\\hsp.txt");//Get a file object, F point to E:ff\hsp.txt file

FileInputStream FIS = null;

try{

FIS = new FileInputStream (f);//defines a byte array, which is equivalent to a cache

byte []bytes = new byte[1024];

int n=0;//to get the actual number of bytes read

while ((N=fis.read (bytes))!=-1) {//loop read

string s = new string (bytes,0,n);//turn Byte into string

System.out.println (s);

}

}catch (Exception e) {

E.printstacktrace ();

}finally{

try{//closed file stream must be placed in the finally statement block

Fis.close ();

}catch (Exception e) {

E.printstacktrace ();

}

}

}

}

4. Receive user input from keyboard and save to file

Import java.io.*;

public class io{

public static void Main (string[] args) {

File F = new file ("E:\\ff\\ss.txt");//overwrite writes the same document directly

FileOutputStream fos = null;

if (f.exists ()) {

System.out.println ("File already exists");

}else{

try{

FOS = new FileOutputStream (f);

String s = "hello,world!\r\n";

String s1= "Chinese";

Fos.write (S.getbytes ());

Fos.write (S1.getbytes ());

}catch (Exception e) {

E.printstacktrace ();

}finally{

try{

Fos.close ();

}catch (Exception E2) {

E2.printstacktrace ();

}

}

}

}

}

5. Picture Copy

Import java.io.*;

public class io{

public static void Main (string[] args) {

FileInputStream FIS = null;//input stream First read the picture into memory before reading to the file

FileOutputStream fos = null;//output stream

try{

fis= new FileInputStream ("e:\\ff\\a.jsp");

FOS = new FileOutputStream ("e:\\a.jsp");

byte buf[] = new byte[1024];

int n=0;//records the number of bytes actually read

while ((N=fis.read (BUF))!=-1) {//loop to read pictures

Fos.write (BUF);//output to the specified file

}

}catch (Exception e) {

E.printstacktrace ();

}finally{

try{

Fis.close ();

Fos.close ();

}catch (Exception e) {

E.printstacktrace ();

}

}

}

}

6. read one file and write to another file char[] to relay

Import java.io.*;

public class io{

public static void Main (string[] args) {

FileReader FR = null;//file Take out character stream object (input stream)

FileWriter FW = NULL;

try{

fr= new FileReader ("E:\\ff\\hsp.txt");//Create Fr Object

FW = new FileWriter ("E:\\hsp.txt");//Create Output Object

Char c[] = new char[1024];

int n=0;//read into memory

while ((N=fr.read (c))!=-1) {

string s = new string (c,0,n);

System.out.println (s);

Fw.write (C,0,n);

}

}catch (Exception e) {

E.printstacktrace ();

}finally{

try{

Fr.close ();

Fw.close ();

}catch (Exception e) {

E.printstacktrace ();

}

}

}

}

7. Buffer character Stream

Import java.io.*;

public class io{

public static void Main (string[] args) {

BufferedReader br = null;

BufferedWriter bw = NULL;

try{

FileReader FR = new FileReader ("E:\\ff\\hsp.txt");

br = new BufferedReader (FR);

FileWriter bw = new FileWriter ("E:\\hsp.txt");

BW = new BufferedWriter (FW);

String s= "";//Loop read

while ((S=br.readline ())!=null) {

Bw.write (s+ "\ r \ n");

}

}catch (Exception e) {

E.printstacktrace ();

}finally{

try{

Br.close ();

Bw.close ();

}catch (Exception e) {

E.printstacktrace ();

}

}

}

}

Hanshunping Java Note 第43-46 Speaking IO programming

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.