节点流(文件流)
字符流
package www.bh.c.iotest;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Test1 {
public static void main(String[] args) {
FileReader fileReader = null;
try {
File file = new File("calss\\hello.txt");
fileReader = new FileReader(file);
int read;
while ((read=fileReader.read())!=-1){
System.out.print((char)read);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader!=null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package www.bh.c.iotest;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) {
FileReader fileReader = null;
try {
File file = new File("calss\\hello.txt");
fileReader = new FileReader(file);
char[] cbuf = new char[3];
int len;
while ((len=fileReader.read(cbuf))!=-1){
String s = new String(cbuf, 0, len);
System.out.print(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader!=null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package www.bh.c.iotest;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Test3 {
public static void main(String[] args) {
FileWriter fileWriter = null;
try {
File file = new File("calss\\hello1.txt");
fileWriter = new FileWriter(file,true);
fileWriter.write("白日依山尽\n");
fileWriter.write("黄河入海流\n");
fileWriter.write("欲穷千里目\n");
fileWriter.write("更上一层楼\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileWriter!=null){try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}}
}
}
}
package www.bh.c.iotest;
import java.io.*;
public class Test4 {
public static void main(String[] args) {
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
File file = new File("calss\\hello1.txt");
File file1 = new File("calss\\hello2.txt");
fileReader = new FileReader(file);
fileWriter = new FileWriter(file1,true);
char[] cbuf=new char[3];
int len;
while ((len=fileReader.read(cbuf))!=-1){
fileWriter.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader!=null)
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileWriter!=null)
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字节流
package www.bh.c.iotest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Test5 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = null;
try {
File file = new File("calss\\hello1.txt");
fileInputStream = new FileInputStream(file);
byte[] c=new byte[3];
int len;
while ((len=fileInputStream.read(c))!=-1){
String s = new String(c, 0, len);
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package www.bh.c.iotest;
import java.io.*;
public class Test6 {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
File file = new File("calss\\Java.png");
File file1 = new File("calss\\Java1.png");
fileInputStream = new FileInputStream(file);
fileOutputStream = new FileOutputStream(file1);
byte[] b=new byte[5];
int len;
while ((len=fileInputStream.read(b))!=-1){
fileOutputStream.write(b,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream!=null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package www.bh.c.iotest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test7 {
public static void main(String[] args) {
Test7 test7 = new Test7();
String srcPath="E:\\Test\\calss\\copyFile\\FileWriter写出数据.avi";
String destPath="E:\\Test\\calss\\copyFile\\FileWriter写出数据1.avi";
long startTime=System.currentTimeMillis();
test7.copyFile(srcPath,destPath);
long endTime=System.currentTimeMillis();
System.out.println("复制花费的时间为:"+(endTime-startTime));
}
public void copyFile(String srcPath,String destPath){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
File file = new File(srcPath);
File file1 = new File(destPath);
fileInputStream = new FileInputStream(file);
fileOutputStream = new FileOutputStream(file1);
byte[] b=new byte[1024];
int len;
while ((len=fileInputStream.read(b))!=-1){
fileOutputStream.write(b,0,len);
}
} catch (
IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream!=null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}