1、input
1、InputStream input=new FileInputStream(path);
2、input.read(b)) != -1
package com.eduask.iodemo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class FileInputStreamDemo {
public static void testInputStream2(String path) {
// 来个流
InputStream in = null;
try {
in = new FileInputStream(path);
int i = 0;
StringBuilder sb = new StringBuilder();
// 读流
while ((i = in.read()) != -1) {
sb.append((char) i);
}
System.out.print(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关流
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
in = null;
}
}
}
}
public static void testInputStream3(String path) {
//新建一个流
InputStream input = null;
try {
input = new FileInputStream(path);
// 读流
int i = 0;
byte[] b = new byte[1024];
StringBuilder sb = new StringBuilder();
while ((i = input.read(b)) != -1) {
sb.append(new String(b));
}
System.out.println(sb);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭流
try {
if (input != null) {
input.close();
}
} catch (IOException e) {
e.printStackTrace();
input=null;
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
testInputStream3("E:\\0302\\workspace\\Day08\\src\\com\\eduask\\iodemo\\FileInputStreamDemo.java");
}
}
2、Ouput
“context”.getBytes()将字符串转成字节
package com.eduask.iodemo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FileOutputStreamDemo {
public static void testFileOut(String path){
OutputStream output=null;
try {
output=new FileOutputStream(path);
String context="世界你好";
try {
output.write(context.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(output!=null){
try {
output.flush();
output.close();
} catch (IOException e) {
e.printStackTrace();
output=null;
}
}
}
}
public static void main(String args[]){
testFileOut("E:\\0302\\filedemo2\\testnihao.txt");
}
}
3、复制
package com.eduask.iodemo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyDemo {
/**
*
* 要读 要写
*
* @param path
*/
public static void testReanAndWriter(String sourcePath, String toPath) {
// 先来个读(输入)的流
InputStream input = null;
// 在来个写的(输出)流
OutputStream output = null;
try {
input = new FileInputStream(sourcePath);
output = new FileOutputStream(toPath);
// 开始读
byte[] b = new byte[1024];
int i = 0;
// 开始写
while ((i = input.read(b)) != -1);{
output.write(b,0,i);
}
output.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (output != null) {
try {
output.flush();//强制输出
output.close();
} catch (IOException e) {
e.printStackTrace();
output = null;
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
input = null;
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
testReanAndWriter(
"E:\\0302\\workspace\\Day08\\src\\com\\eduask\\iodemo\\CopyDemo.java",
"E:\\CopyDemo3.java");
}
}