Io stream file read, write, and copy __java in Java

Source: Internet
Author: User

Construct File class

File F=new file (fileName);

To determine whether a directory

F.isdirectory ();

Get the file name under the directory

String[] Filename=f.list ();

Get the files in the directory
File[] Files=f.listfiles ();


1, Java How to read files

Package com.yyb.file;

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.InputStream;

*
 * Read the file:
 * 1, find the specified file
 * 2, according to the file creation file input stream
 * 3, create byte array
 * 4, read content, put in byte array
 * 5, close the input stream
* * public class Fileread {public

	static void Main (string[] args) {
		//build the specified file
		= filename = new ("E:" + file . Separator + "Hello.txt");
		InputStream in = null;
		try {
			//The input stream in
			= new FileInputStream (file) based on file creation files;
			Create byte array
			byte[] data = new byte[1024];
			Read the content and put it into the byte array
			in.read (data);
			System.out.println (new String (data));
		catch (Exception e) {
			e.printstacktrace ();
		} finally {
			try {
				//Close input stream
				in.close ();
			} catch (Exception e) {
				e.printstacktrace ();}}}




2, Java How to write files

Package com.yyb.file;

Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.OutputStream;

*
 * Write to File:
 * 1, find the specified file
 * 2, according to file creation file output stream
 * 3, convert content to byte array
 * 4, write content to file
 * 5, close the input
 stream
public class FileWriter {public

	static void Main (string[] args) {
		//build the specified file
		= filename = new ("E:" + Fi Le.separator + "Hello.txt");
		OutputStream out = null;
		try {
			//The output stream of the file created by the file
			= new FileOutputStream (file);
			String message = "I am a good man." ";
			Convert content to byte array
			byte[] data = Message.getbytes ();
			Write content to file
			out.write (data),
		catch (Exception e) {
			e.printstacktrace ();
		} finally {
			try {
				//Turn off output stream
				out.close ();
			} catch (Exception e) {
				e.printstacktrace ()

}}}}



3, Java How to copy files

<span style= "FONT-SIZE:18PX;"

>package Com.yyb.file;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream; * * Implementation of ideas: * 1, build source file and target file * 2, source file Create input stream, target file Create output stream * 3, create byte array * 4, use loop, source file read part of content, target file write part of content, until all content is finished * 5, close source file input stream, target File output stream/public class FileCopy {public static void main (string[] args) {//build source files = new file ("E:" + file
		. Separator + "HelloWorld.txt");
		Build target file FileCopy = new file ("D:" + file.separator + "HelloWorld");
		InputStream in = null;
		OutputStream out = null; The try {//destination file does not exist to create an if (!) (
			Filecopy.exists ()) {filecopy.createnewfile ());
			///source file Create input stream in = new FileInputStream (file);
			Target file creation output stream out = new FileOutputStream (FileCopy, true);
			Create byte array byte[] temp = new byte[1024];
			int length = 0;
			The source file reads part of the content while (length = in.read (temp))!=-1) {//The target file is written to part Out.write (temp, 0, length); }} CATCH (Exception e) {e.printstacktrace ();
				Finally {try {//close file Input output stream in.close ();
			Out.close ();
			catch (Exception e) {e.printstacktrace (); }}}</span><span style= "FONT-SIZE:24PX;" > </span>



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.