1,文件的编码:
string s=”呵呵abc”;
//把字符串转换成字节数组,一个byte(字节)占8位二进制编码。
byte[] bytes=s.getBytes();
for(byte b : bytes){
//这里把一个字节(8位2进制的编码,转换为int型的32位2进制编码,然后最后变成8位16进制编码)————————–输出有效的两位
system.out.print(Integer.stoHexString(b & 0xff));
}
btte[] bytes=s.getBytes(“gbk”);中文占两个字节,英文占一个字节
byte[] bytes=s.getBytes(“utf-16be”);//中英文都占两个字节
byte[] bytes=s.getBytes(“utf-8”);//中文占三个字节,英文占一个字节
如果把字节序列转换成对应的字符串,必须进行对应的字节编码,不然会出现乱码的情况
String s=new String(bytes , “utf-8”);
String s=new String(bytes , “gbk”);
String s=new String(bytes , “utf-16be”);
2,java.IO.File类:用于表示文件(目录)
Flie类只用于表示文件(目录)的信息(名称,大小等),不能访问文件里面的内容。
File.getName():获取文件(目录)名
File.getParent():获取上一级的文件(目录)名
一个文件遍历工具类
import java.io.File;
import java.io.IOException;
public class FileUtils {
public static void listDirectory(File dir) throws IOException {
//判断dir存不存在
if(!dir.exists()){
throw new IllegalArgumentException("目录"+dir+"不存在");
}
//判断是不是目录(如果是文件的话)
if(!dir.isDirectory()){
throw new IllegalArgumentException(dir+"不是目录");
}
/*//这里是获取dir当前目录下的文件名字,返回字符串数组
String[] filenames=dir.list();
for(String name:filenames){
System.out.println(name+" ");
}*/
//当前目录的直接子目录的文件
File[] files=dir.listFiles();
if(files!=null&&files.length>0){
for(File file : files){
if(file.isDirectory()){
listDirectory(file);
}
else {
System.out.println(file);
}
}
}
}
}
3,RandomAccessFile类
RandomAccessFile提供对文件内容的访问,读写
RandomAccessFile支持随机访问文件,可以访问任意的位置
(1)java的文件模型:
在硬盘是事以byte byte byte存储的,是数据的集合
(2)打开文件:
有两种方式:“rw”(读写),”r”(只读)
RandomAccessFile raf=new RandomAccessFile(file,”rw”);
文件指针,打开文件的时候文件指针是pointer=0
(3)写文件
raf.write(int ):写入一个字节(后8位)同时指针指向下一个位置,准备再次写入
(4)读文件
int i=raf.read():读一个字节
(5)读写文件以后必须要关闭,不然会有意想不到的错误。
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
public class RafTest {
private static RandomAccessFile raf;
public static void main(String[] args) throws IOException {
File deom=new File("demo");
if(!deom.exists()){
deom.mkdirs();
}
File file=new File(deom,"raf.txt");
if(!file.exists()){
file.createNewFile();
}
raf=new RandomAccessFile(file, "rw");
raf.write('A');//这里只写了一个字节
String s="中";
byte[] bytes=s.getBytes("gbk");//两个字节
raf.write(bytes);//写入字节
//读文件:必须从文件开始读取
raf.seek(0);
//一次性读取,把文件中的内容全部读到字节数组中
byte[] buf=new byte[(int) raf.length()];//这里是获取一个字节数组的长度(文件的字节长度相同)
//然后通过这个文件的read()方法把这个文件的内容读到buf这个字节数组里面
raf.read(buf);
System.out.println(Arrays.toString(buf));
String str=new String(buf,"gbk");
System.out.println(str);
raf.close();
}
}
4.IO流
字节流,字符流:
//这里是从键盘的输入流
字节流:inputstream,outputstream
inputstream类:抽象了应用程序读取数据的方式
outputstream类:抽象了应用程序写出数据的方式
读到结尾:eof==end eof==-1
inputstream输入流:主要是读。
基本方法:
int b=in.read()读取一个字节,无符号填充到int的后8位
读到-1就结束
in.read(byte[] buf)直接读到一个字节数组里面去
in.read(byte[] buf,int start ,int size)读取到buf字节数组,从start位置开始,读取长度为size
*****outputstream输出流:主要是写.
out.write(int b)写出一个byte到流,b的低8位
out.write(byte[] buf)把buf数组写入到流
out.write(byte[] buf,int start,int size)
//文件的输入流
FileInputStream 文件输入流:具体实现了在文件上读取数据*
//该方法返回数据的下一个字节,或如果到达文件的结尾则为-1。
in.read()
import java.io.FileInputStream;
import java.io.IOException;
public class IOUtils {
public static void FileRead(String name) throws IOException{
FileInputStream in=new FileInputStream(name);
int b;
while((b=in.read())!=-1){
System.out.println("int--->"+b);//b这里是这个字节对应的编码(10进制编码)
System.out.println(Integer.toHexString(b & 0xff));
}
in.close();
}
}
byte[] buf=new byte[20*1024];
int bytes=in.read(buf,0,buf.length());返回读取的字节数,放到buf字节数组里面去。
FileOutputStream类:向文件写出byte数据
out.write()只能写一个字节,写出了低8位
out.write(‘A’)写出了A的低8位
byte[] gbk=”中国”.getBytes(“gbk”);
out.write(gbk);
out.close();
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOUtils {
public static void FileRead(String name) throws IOException{
FileInputStream in=new FileInputStream(name);
int b;
while((b=in.read())!=-1){
System.out.println("int--->"+b);//b这里是这个字节对应的编码(10进制编码)
System.out.println(Integer.toHexString(b & 0xff));
}
in.close();
}
public static void copyFile(File srcFile,File destFile) throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException(srcFile+"文件不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
int bytes;
byte[] buffer = new byte[8*1024];
FileInputStream in=new FileInputStream(srcFile);
//这里如destfile文件没有,自动创建,如果有,先删除后,创建
FileOutputStream out = new FileOutputStream(destFile);
//返回读取的字节个数,读到buffer这个字节数组中去,最多读buffer.length个字节,读到结尾返回-1
while((bytes=in.read(buffer,0,buffer.length))!=-1){
out.write(buffer,0,bytes);
out.flush();
}
in.close();
out.close();
}
}
5,DataInputStream和DataOutPutStream对流的功能的扩展,对int,long 等其他的类型更方便操作
DataOutputStream:方法:dos.writeInt(),writeLong(),writeUTF()
6,BufferedInputStream和BufferedOutputStream用来提高IO的性能。
二:字符流
文本和文本文件:
java文本(char)是16位无符号整数,一字符unicode编码(双字节编码),以byte,byte,byte的数据序列
文本(char)文件已某种编码(utf-8,gbk,utf-16be等)序列化为byte存储。
InputStreamReader:字符输入流:完成从byte流解析为char流
OutputStreamWriter:字符输出流:完成充char流解析为byte流
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Reader {
public static void main(String[] args) throws IOException{
FileInputStream in=new
//字节流
FileInputStream("e:/hhh/1.txt");
//解析为字符流
InputStreamReader isr=new InputStreamReader(in);
int c;
String content="";
//字符输入流对应的字符数组
char[] chars=new char[8*1024];
//返回下一个字节数据
while((c=isr.read(chars))!=-1){
content+=new String(chars);
}
System.out.println(content);
}
}
字符过滤器
BufferedReader —> readLine:一次读一行
BufferedWriter/PrintWriter—->写一行
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedReaderAndBufferedWriter {
public static void main(String[] args) throws IOException{
//字节流
FileInputStream fileInputStream=new FileInputStream("e:/hhh/1.txt");
//字节流转换成字符流
InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
String line;
while((line=bufferedReader.readLine())!=null){
System.out.println(line);
}
}
}
7,对象的序列化:将object转化为byte序列,就是对象序列化,反之为反序列化
序列化流(objectoutputstream)是过滤流——–writeObject
反序列化流(objectinputstream)是过滤流—–readObject
对象序列化接口(serializeable):对象必须实现序列化接口才能序列化,不然会有异常,这个接口没有方法,只是一个标准。
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectInputStreamAndObjectOutputStream{
public static void main(String[] args){
}
public void writer() throws IOException{
Student stu=new Student("hhh", 123, 123);
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("e:/hhh/2.txt"));
oos.writeObject(stu);
oos.close();
}
public void reader() throws IOException, ClassNotFoundException{
FileInputStream fis=new FileInputStream("e:/hhh/2.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Student s=(Student) ois.readObject();
System.out.println(s.getId());
ois.close();
}
}