java I/O的基本使用

时间:2023-03-10 05:11:03
java I/O的基本使用

1.什么是I/O

  a.I/O也称为:输入输出,可以理解为In,Out

  b.I/O流:读取键盘键入的字符,硬盘上的文件

  c.处理数据的类型分类:字节流、字符流

  字节流:以Stream结尾的,可以处理图片、文字、音频、视频等,读取一个字节返回一个字节。

  字符流:以Reader或Writer结尾,仅能处理纯文本数据,读取一个或多个字节,再查询指定编码表,最后返回字符。

  字节流代码实例演示:

  

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; /**
* Created by Pres_cheng on 2016/9/4.
*/
public class UsingFileStream {
public static void main(String args[]) {
File file = new File("D:\\Ideaworkspace\\UsingFileStream\\src\\hello.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileInputStream fis = new FileInputStream(file);
byte[] bytes = new byte[1024];
String str = "";
while (fis.read(bytes) != -1) {
str += new String(bytes,"utf-8");
}
System.out.println(str);
//每次使用完流后都要关闭
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; /**
* Created by Pres_cheng on 2016/9/4.
*/
public class UsingFileOutStream {
public static void main(String args[]){
//写入的数据
String str = "大家好,我是学生123456abc";
//文件的路径
File file =new File("D:\\Ideaworkspace\\UsingFileStream\\src\\hello");
if(!file.exists()){
try {
//如果不存在就创建文件
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
//实例化输出流
FileOutputStream fos = new FileOutputStream(file);
byte [] btyes = str.getBytes();
fos.write(btyes);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

  复制图片

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; /**
* Created by Pres_cheng on 2016/9/4.
*/
public class CopyFile {
public static void main(String args[]){ try {
FileInputStream fis = new FileInputStream("D:\\Ideaworkspace" +
"\\UsingFileStream\\img.jpg");
FileOutputStream fos = new FileOutputStream("D:\\Ideaworkspace" +
"\\UsingFileStream\\newImg.jpg");
byte [] bytes = new byte[1024];
while (fis.read(bytes) != -1){
fos.write(bytes);
}
fis.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

  

  字符流实例演示:

  

import java.io.*;

/**
* Created by Pres_cheng on 2016/9/4.
*/
public class UsingStreamReader {
public static void main(String[] args) {
try {
//实例化字节流
FileInputStream fis = new FileInputStream("D:\\Ideaworkspace" +
"\\UsingFileReaderAndWriter\\src\\test.txt");
//将字节流转化给字符流
InputStreamReader isr = new InputStreamReader(fis, "utf-8");
FileOutputStream fos = new FileOutputStream("D:\\Ideaworkspace" +
"\\UsingFileReaderAndWriter\\src\\newTest.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
char[] chars = new char[100];
int i;
// String str = "";
while ((i = isr.read(chars)) != -1){
// str += new String(chars,0,i);
osw.write(chars,0,i);
}
// System.out.println(str);
//关闭流,按照先打开后关闭后打开先关闭的原则
osw.close();
fos.close();
isr.close();
fis.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}