FileInputStream是Java语言中抽象类InputStream用来具体实现类的创建对象。FileInputStream可以从文件系统中的某个文件中获得输入字节,获取的文件可用性取决于主机环境。(输入流 字节流 节点流)
FileInputStream通过打开与实际文件的连接来创建一个文件,该文件由文件系统中的File 对象命名file。
1
| public FileInputStream(File file)
|
FileInputStream通过使用文件描述符创建一个,该描述符 fdObj表示与文件系统中实际文件的现有连接。
1
| public FileInputStream(FileDescriptor fdObj)
|
FileInputStream通过打开与实际文件的连接来创建一个文件,该文件由name 文件系统中的路径名命名。
1
| public FileInputStream(String name)
|
FileInputStream操作文本文件时并在控制台打印时的问题

FileOutputStream
FileOutputStream流是指文件字节输出流,专用于输出原始字节流如图像数据等,其继承OutputStream类,拥有输出流的基本特性(输出流 字节流 节点流)
创建文件输出流以写入由指定File对象表示的文件。
1
| public FileOutputStream(File file)
|
创建文件输出流以写入指定的文件描述符,该文件描述符表示到文件系统中实际文件的现有连接。
1
| public FileOutputStream(FileDescriptor fdObj)
|
创建文件输出流以写入由指定File对象表示的文件。
1
| public FileOutputStream(File file, boolean append)
|
创建文件输出流以写入具有指定名称的文件。
1
| public FileOutputStream(String name)
|
创建文件输出流以写入具有指定名称的文件。
1
| public FileOutputStream(String name, boolean append)
|
FileInputStream和FileOutputStream操作非文本文件

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| import org.junit.Test;
import java.io.*;
public class FileInputOutputStreamTest {
@Test public void testFileInputStream() { FileInputStream fis = null; try { File file = new File("hello1.txt"); fis = new FileInputStream(file);
byte[] buffer = new byte[5]; int len; while ((len = fis.read(buffer)) != -1) { String str = new String(buffer, 0, len); System.out.print(str); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } }
@Test public void testFileInputOutputStream() { FileInputStream fis = null; FileOutputStream fos = null; try { File srcFile = new File("爱情与友情.jpg"); File descFile = new File("爱情与友情2.jpg");
fis = new FileInputStream(srcFile); fos = new FileOutputStream(descFile);
byte[] buffer = new byte[5]; int len; while ((len = fis.read(buffer)) != -1) { fos.write(buffer, 0, len); } System.out.println("复制成功!"); } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public void copyFile(String srcPath, String descPath) {
FileInputStream fis = null; FileOutputStream fos = null; try { File srcFile = new File(srcPath); File descFile = new File(descPath);
fis = new FileInputStream(srcFile); fos = new FileOutputStream(descFile);
byte[] buffer = new byte[1024]; int len;
while ((len = fis.read(buffer)) != -1) { fos.write(buffer, 0, len); } System.out.println("复制成功!"); } catch (IOException e) { e.printStackTrace(); } finally {
if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } }
} }
@Test public void testCopyFile(){
long start = System.currentTimeMillis();
copyFile("C:\\Data\\素材库\\视频\\48370019\\588\\48370019_588_0.flv","视频.flv");
long end = System.currentTimeMillis();
System.out.println("复制成功花费的时间为:"+(end-start)); } }
|