An example of IO in Java

Source: Internet
Author: User

character Stream

Package jd_1;

Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;

public class Jd_1 {

/**
* Character Stream
*
* @param args
*/
public static void Main (string[] args) {
Create BufferedReader for reading files
BufferedReader reader = null;
Create bufferedwriter for writing files
BufferedWriter writer = null;
Create FileReader to save read-in paths
FileReader FIS = null;
try {
FIS = new FileReader ("F:\\java io\\ simple Copy\\source.txt");
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Create FileWriter to save the write path
FileWriter FW = NULL;
try {
FW = new FileWriter ("F:\\java io\\ simple Copy\\targetcopy.txt");
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
reader = new BufferedReader (FIS);
writer = new BufferedWriter (FW);
String line = null;
Reading is a string
try {
while (line = Reader.readline ()) = null) {
try {
Thread.Sleep (500);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
// }
Writer.write (line);
// }
Use an array as a broker
Char[] C = new char[1024];
StringBuffer buffer = new StringBuffer ();
int legin = Fis.read (c);
while (legin! =-1) {
Buffer.append (c);
Legin = Fis.read ();
Fw.write (c);
}
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} finally {
System.err.println ("copy succeeded");
if (reader! = null) {
try {
Reader.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
if (writer! = null) {
try {
Writer.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
}

BYTE stream

public class Copy {
public static void Main (string[] args) {
Before copying
FileInputStream file = null;
After copying
FileOutputStream file1 = null;
try {
The path before the copy
File = new FileInputStream ("F:\\java io\\ new text document. txt");
The path after the copy
File1 = new FileOutputStream ("F:\\java io\\copy.txt");
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Create an array of characters as a broker
byte[] Words = new byte[1024];
Record the length of an array
int Len;
try {
while (len = File.read (Words))! =-1) {
File1.write (Words, 0, words.length);
System.out.println ("copy succeeded");
}
} catch (IOException e) {

E.printstacktrace ();
} finally {
if (file! = null) {
try {
File.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
if (file1! = null) {
try {
File1.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
}

Binary stream

Package bdqn4;

Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;

/**
* Binary Read
*
* @author Administrator
*
*/
public class Text {
public static void Main (string[] args) {
Create a Stream
Implementing Read and Write operations
Close the stream

DataInputStream dis = null;
DataOutputStream dos = null;

FileInputStream FIS = null;
try {
FIS = new FileInputStream ("F:\\java io\\ original picture. jpg");
} catch (FileNotFoundException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
FileOutputStream fos = null;
try {
FOS = new FileOutputStream ("F:\\java io\\copy picture \\copy.jpg");
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
dis = new DataInputStream (FIS);
DOS = new DataOutputStream (FOS);
int temp;
try {
while (temp = Dis.read ())! =-1) {
Dos.write (temp);
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} finally {
SYSTEM.ERR.PRINTLN ("transfer success");
if (dis! = null) {
try {
Dis.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
if (dos = null) {
try {
Dos.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
}

Determine the various properties and methods of IO

Package bdqn1;

Import Java.io.File;
Import java.io.IOException;
Import Java.util.Scanner;

public class Text {
public static void Main (string[] args) {
Scanner input = new Scanner (system.in);
Instantiating objects pointing to determine the path
File File = new file ("F:\\java io\\ new text document. txt");
Determine if the TET exists.
if (file.exists ()) {
System.out.println ("Current file exists");
SYSTEM.OUT.PRINTLN ("Full path to File" + File.getabsolutepath ());
SYSTEM.OUT.PRINTLN ("filename" + file.getname ());
SYSTEM.OUT.PRINTLN ("Relative path to file" + File.getpath ());
SYSTEM.OUT.PRINTLN ("Top level directory of files" + file.getparent ());
System.out.println ("Length of File" + file.length ());
if (File.isdirectory ()) {
System.out.println ("Current is a folder");
} else {
System.out.println ("Current is not a folder");
System.err.println ("Please enter 1 to delete");
int number = Input.nextint ();
if (number = = 1) {
boolean bool = File.delete ();
if (bool) {
System.out.println ("delete succeeded");
}
}
}
} else {
System.out.println ("Current file does not exist");
try {
boolean bool = File.createnewfile ();
if (bool) {
SYSTEM.OUT.PRINTLN ("created successfully");
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}

An example of IO in Java

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.