字节流示例
/*字节流*/
public class TestByteStream {
public static void main(String[] args) throws IOException {
// testFileOutputStream();
// testFileInputStream();
copyContentFormSource2Target();
}
public static void testFileOutputStream() throws IOException {
//1、创建目标对象,输出流表示把数据保存到哪个文件。不写盘符,默认该文件是在该项目的根目录下
File file = new File("io"+File.separator+"test.txt");
System.out.println(file.getAbsolutePath());
if(!file.exists()){
file.createNewFile();
}
OutputStream outputStream = null;
try {
//2、创建文件的字节输出流对象,第二个参数是 Boolean 类型,true 表示后面写入的文件追加到数据后面,false 表示覆盖
outputStream = new FileOutputStream(file,false);
outputStream.write(65);
outputStream.write("csc".getBytes());
outputStream.write("helloworld".getBytes(),1,4);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if(outputStream != null){
outputStream.close();
}
}
}
public static void testFileInputStream() throws IOException {
File source = new File("io"+File.separator+"test.txt");
InputStream inputStream = null;
try {
System.out.println("【测试1】");
inputStream = new FileInputStream(source);
int data1 = inputStream.read();
System.out.println((char)data1);
System.out.println("【测试2】");
byte[] data2 = new byte[10];
int numBytesRead = inputStream.read(data2);
System.out.println(Arrays.toString(data2));
System.out.println(new String(data2));
System.out.println(numBytesRead);
System.out.println("【测试3】");
int num = inputStream.read(data2,0,3);
System.out.println(Arrays.toString(data2));
System.out.println(new String(data2));
System.out.println(num);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
inputStream.close();
}
}
public static void copyContentFormSource2Target() throws IOException {
File source = new File("io"+File.separator+"source.txt");
File target = new File("io"+File.separator+"target.txt");
if(!source.exists()){
source.createNewFile();
}
if(!target.exists()){
target.createNewFile();
}
InputStream input = new FileInputStream(source);
OutputStream out = new FileOutputStream(target);
byte[] data = new byte[10];
// int available = Math.min(10,input.available());
// while (available > 0){
// input.read(data,0,available);
// out.write(data,0,available);
// available = Math.min(10,input.available());
// }
int len;
while((len = input.read(data))!=-1){
out.write(data,0,len);
}
input.close();
out.close();
}
}
上述代码作用是将文本写入项目目录下的io子目录下的txt文件,然后从中读取。
字符流示例
/*字符流*/
public class TestCharStream {
public static void main(String[] args) throws IOException {
// testWriter();
testReader();
// copyFile();
}
private static void testWriter() throws IOException {
File file = new File("io"+File.separator+"sourceWriter.txt");
if(!file.exists()){
file.createNewFile();
}
FileWriter writer = new FileWriter(file);
writer.write(65);
writer.write("我是");
writer.write("changshuchao".toCharArray());
writer.write(",helloa",0,6);
writer.write(",nice to meet you , haha".toCharArray(),0,17);
/***
* 注意如果这里有一个 缓冲的概念,如果写入文件的数据没有达到缓冲的数组长度,那么数据是不会写入到文件中的
* 解决办法:手动刷新缓冲区 flush()
* 或者直接调用 close() 方法,这个方法会默认刷新缓冲区
*/
writer.flush();
writer.close();
}
private static void testReader() throws IOException {
File file = new File("io"+File.separator+"sourceWriter.txt");
if(!file.exists()){
file.createNewFile();
}
FileReader reader = new FileReader(file);
int len;
while((len = reader.read())!= -1){
System.out.println((char)len);
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
char[] data = new char[10];
while((len = reader.read(data)) != -1){
System.out.println("["+new String(data,0,len)+"]");
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
while((len = reader.read(data,0,10))!= -1){
System.out.println(new String(data,0,len));
}
}
private static void copyFile() throws IOException {
File source = new File("io"+File.separator+"sourceWriter.txt");
if(!source.exists()){
source.createNewFile();
}
File target = new File("io"+File.separator+"targetWriter.txt");
if(!target.exists()){
target.createNewFile();
}
FileReader reader = new FileReader(source);
FileWriter writer = new FileWriter(target);
int len = -1;
char[] data = new char[10];
while((len = reader.read(data))!=-1){
writer.write(data,0,len);
}
reader.close();
writer.close();
}
}