Home  >  Article  >  Java  >  iO流

iO流

巴扎黑
巴扎黑Original
2017-06-26 11:37:391843browse

文件编码:

①gbk编码:中文占用2个字节,英文占用1个字节

②utf-8编码:中文占用3个字节,英文占用1个字节
③java是双字节编码 utf-16be,中文占用2个字节,英文占用2个字节
④当你的字节序列是某种编码时,若想把该字节序列变成字符串,也必须用这种编码方式,不能直接使用String str = new String(byteStr);否则系统会使用默认的编码,从而出现乱码
⑤文本文件就是字节序列,可以是任意编码的字节序列,如果我们在中文机器上直接创建文本文件,则该文本文件只认识ansi编码
在eclipse项目下创建的项目可以指定项目的编码形式。eclipse默认的“GBK”编码不能识别“utf-8”的文件。若是将eclipse中创建的“utf-8”文件复制到电脑上是可以识别的,但是!在电脑本地上创建的“utf-8”文件,电脑识别不了

File类常用API的使用:

java.io.File类在Java中表示文件或目录。
File类只用于表示文件(目录)的信息(名称、大小等),不能用于文件内容的访问。
创建File对象:File file=new File(String path);
两种创建“日记1.txt”文件的方法
<br>
可以通过 alt+/ 查看构造函数
注意:盘符后跟双斜杠 \\ 或者 /

1. file.seperater();获取系统分隔符
2. file.exists();是否存在.存在返回true,不存在返回false
3. file.mkdir();或者file.mkdirs();创建目录或多级目录。
4. file.isDirectory()或者file.isFile()判断是否是目录或者是否是文件。是返回true,不是为false
5. file.delete();删除文件/文件夹。
6. file.createNewFile();创建新文件。
7. file.getName()获取文件名称。
8. file.getAbsolutePath()获取绝对路径。
9. file.getParent();获取父级绝对路径

file类的遍历目录

File对象 dir
 dir.isDirectory() 判断当前路径是否为目录
dir.list()返回String[],用于列出当前目录下的子目录和文件(不包含子目录下的名称)
如果要遍历子目录下的内容就需要构造成File对象做递归操作
File files = dir.listFiles();//返回的是直接子目录(文件)的抽象

RomdonAccessFile基本操作

Java提供的对文件内容的访问,可以读内容也可以写内容
支持随机访问文件,可以访问文件的任意位置
(1)Java文件模型
在硬盘上是byte bytebyte存储的,是数据的集合
(2)打开文件
有两种模式“rw”读写 和“r”只读
RondomAccessFile raf = new RondomAccessFile(file,”rw”);
文件指针  打开文件时指针在开头pointer = 0;
(3) 写方法
Raf.writer(int)---à只写一个字节(后八位),同时指针指向下一个位置,准备再次写入
(4)读方法
int b = raf.read()--à从指针在的位置读一个字节
(5)文件读写完之后一定要关闭
(6)代码
public static void main(String[] args) throws IOException{
    File demo = new File("demo");//新建一个File对象,如果没有写绝对路径则是默认在项目路径下
    if(!demo.exists())
        demo.mkdir();
    File file = new File(demo,"raf.dat");//在demo下创建文件raf.dat
    if(!file.exists())
        file.createNewFile();//在项目下新建了demo文件夹,其中有raf.dat文件
    <br>
    RandomAccessFile raf = new RandomAccessFile(file,"rw")//创建读内容的对象raf
    //指针的位置,写一个字节指针移一位
    System.out.println(raf.getFilePointer());   //开始时在控制台输出0
raf.write('A');//只写了一个字节(后8位)
System.out.println(raf.getFilePointer());//输出1
int i = 0x7fffffff;//用write方法每次只能写一个字节,若要把i写进去就得写4次

 

<br>
此时输出6
raf.writeInt(i);//直接写一个int
System.out.println(raf.getFilePointer());//输出10
    String s = "中";
    byte[] gbk = s.getBytes("gbk");
    raf.write(gbk);
System.out.println(raf.getFilePointer());//输出12 中文两个字节
 <br>
    //读文件,必须把指针移到头部
    ref.seek(0);
    //一次性读取,把文件中的内容都读到字节数组中
    byte[] buf = new byte[(int)raf.length];
    raf.read(buf);
System.out.println(Array.toString(buf));
 <br>
//用十六进制包装   重新输入
    for(byte b:buf){
        System.out.println(Integer.toHexString(b & 0xff)+"");
}
 <br>
//最后一定要加上close()方法
}

五、字节流

读写文件时以字节为单位
1) 有两个父类:InputStream和OutputStream
   InputStream是抽象类,抽象了应用程序读取数据的方式
   OutputStream抽象了应用程序写出数据的方式
2) EOF = End 或 读到-1就读到结尾
3) 输入流基本方法(in是输入流对象)
   int b = in.read();//读取一个字节无符号填充到int低八位,-1是EOF
   in.read(byte[] buf)  读取数据填充到字节数组buf
   int.read(byte[] buf,int start,int size)  读取数据到字节数组buf,从buf的start位置开始存放size长度的数据
4) 输出流基本方法(进行写的操作)
    out.write(int b) 写出一个byte到流,b的低八位
    out.write(byte[] buf) 将buf字节数组都写入到流
    out.write(byte[] buf,int start,int size) 字节数组buf从start位置开始写size长度的字节到流

The above is the detailed content of iO流. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:SpringMvc file uploadNext article:SpringMvc file upload