Java IO学习笔记二

流的概念

字节流和字符流

操作流程

字节流

FileOutputStream

构造函数

常用的方法

实例

package File_demo;

import java.io.*;

public class demo {
public static void main(String[] args) {
FileOutputStream outputStream = null;
File file = new File("/tmp" + File.separator + "test.txt");
try {
outputStream = new FileOutputStream(file);
try {
int data = 48;
String name = "陈加兵\n"; //使用\n换行
byte[] bytes = name.getBytes(); //将字符串转换成byte数组
outputStream.write(bytes, 0, 3); //将中文字符串的第一个字写入,这里一个中文占了三个字节
String age = "chenjiabing\n";
outputStream.write(age.getBytes());
outputStream.write(data); //这里的写入的acsii码中的( } catch (IOException e) {
e.printStackTrace();
} } catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close(); //关闭文件流
} catch (IOException e) {
e.printStackTrace();
} }
} }
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream; public class Test11 {
public static void main(String[] args) throws IOException {
File f = new File("d:" + File.separator+"test.txt");
OutputStream out=new FileOutputStream(f);//如果文件不存在会自动创建
String str="Hello World";
byte[] b=str.getBytes();
for(int i=0;i<b.length;i++){
out.write(b[i]);
}
out.close();
}
}

FileInputStream

构造函数

常用方法

实例

File file=new File("/tmp"+File.separator+"test.txt");
FileInputStream inputStream=null;
try {
inputStream=new FileInputStream(file); try {
byte[] bytes=new byte[(int)file.length()]; //file.length返回文件的大小,这样就不会浪内存空间了
int flag=inputStream.read(bytes); //将文件的内容读入到数组中
System.out.println(new String(bytes)); //将bytes数组转换成字符串输出
System.out.println(flag);
}catch (IOException e)
{
e.printStackTrace();
}
}catch (FileNotFoundException e)
{
e.printStackTrace();
}finally {
if(inputStream!=null)
{
try {
inputStream.close();
}catch (IOException e)
{
e.printStackTrace();
} }
}
File file=new File("/tmp"+File.separator+"test.txt");
FileInputStream inputStream=null;
try {
inputStream=new FileInputStream(file); try {
int len=0; //读取的字节
int i=0; //下标
byte[] bytes=new byte[(int)file.length()]; //创建数组
while((len=inputStream.read())!=-1) //判断是否读取到文件的末尾
{
bytes[i]=(byte)len; //将读到的整形数据转换成bytes类型的,存储在数组中
i++;
}
System.out.println(new String(bytes)); //转换成字符串 }catch (IOException e)
{
e.printStackTrace();
}
}catch (FileNotFoundException e)
{
e.printStackTrace();
}finally {
if(inputStream!=null)
{
try {
inputStream.close();
}catch (IOException e)
{
e.printStackTrace();
} }
}
File file = new File("/tmp" + File.separator + "test.txt");
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file); try { byte[] bytes = new byte[(int) file.length()]; //file.length返回文件的大小,这样就不会浪内存空间了
int i = 0;
while (inputStream.available() != 0) {
try {
bytes[i] = (byte) inputStream.read();
i++;
} catch (IOException e) {
e.printStackTrace();
} }
System.out.println(new String(bytes)); } catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
} }
}

字符流

字符输出流

FileWriter

构造函数

常用方法

实例

File file=new File("/tmp"+File.separator+"test"+File.separator+"test.txt");
File f1=new File("/tmp"+File.separator+"test");
if(!f1.exists())
{
f1.mkdir();
System.out.println("文件创建成功");
}
FileWriter fileWriter=null; try {
fileWriter=new FileWriter(file);
String str="hello chenjiabing\n";
String name="陈加兵";
int data=48;
fileWriter.write(str); //写入字符串
fileWriter.write(name); //写入中文字符串,这里直接写入不用转换成byte数组了
fileWriter.write(data); //写入单个字符
}catch (IOException e)
{
e.printStackTrace();
}finally {
if(fileWriter!=null)
{ try {
fileWriter.flush(); //刷新缓冲区
fileWriter.close(); //关闭字符流
}catch (IOException e)
{
e.printStackTrace();
}
}
}

字符输入流

FileReader

构造函数

常用函数

实例

File file = new File("/tmp" + File.separator + "test" + File.separator + "test.txt");
FileReader fileReader = null;
try {
fileReader = new FileReader(file);
char[] c = new char[(int) file.length()]; //根据文件的大小申请数组大小,不浪费
try {
int len = fileReader.read(c); //将文件的内容读取到字符数组中
for (int i = 0; i < c.length; i++) {
System.out.println(c[i]); //将一个一个字符输出
}
} catch (IOException e) {
e.printStackTrace();
} } catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
File file = new File("/tmp" + File.separator + "test" + File.separator + "test.txt");
FileReader fileReader = null;
try {
fileReader = new FileReader(file);
char[] c = new char[(int) file.length()]; //根据文件的大小申请数组大小,不浪费
try {
while(fileReader.ready()) //判断是否读到文件末尾
{
System.out.println((char)fileReader.read()); //转换成字符
}
} catch (IOException e) {
e.printStackTrace();
} } catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

参考文章

04-15 11:49