fileoutputstreamdemo.java

来自「Java网络编程与分布式计算, 主要分析java网络各方面的编程, 提供许多实用」· Java 代码 · 共 55 行

JAVA
55
字号
import java.io.*;

// Chapter 4, Listing 2
public class FileOutputStreamDemo 
{
	public static void main(String args[])
	{
		// Two parameters are required, the source and destination
		if (args.length != 2)
		{
			System.err.println ("Syntax - FileOutputStreamDemo src dest");
			return;
		}

		String source = args[0];
		String destination = args[1];

		try
		{
			// Open source file for input
			InputStream input = new FileInputStream( source );

			System.out.println ("Opened " + source + " for reading.");

			// Ouput output file for output
			OutputStream output = new FileOutputStream ( destination );

			System.out.println ("Opened " + destination + " for writing.");

			int data = input.read();

			while ( data != -1)
			{
				// Write byte of data to our file
				output.write (data);

				// Read next byte
				data=input.read();
			}

			// Close both streams
			input.close();
			output.close();

			System.out.println ("I/O streams closed");
		}
		catch (IOException ioe)
		{
			System.err.println ("I/O error - " + ioe);
		}
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?