一.File 属性

Java.io包,但不是InputStream,OutputStream的子类

创建File对象

1.	FIle name = new File(String path)
2.	FIle name = new File(String path,string name)
3.	FIle name = new File(File dir ,String name)

path 对应 磁盘的文件或者目录名

name 表示文件名,目录名

dir为代表磁盘目录对象 表示文件或者目录的路径

文件目录属性

public static void main(String[] args) {
      File file = new File("G:/FFOutput/test.txt");
      System.out.println(file.getName());//获取名字
      System.out.println(file.getPath());//获取相对路径
      System.out.println(file.getAbsolutePath());//获得绝对路径
      System.out.println(file.getParent());
      System.out.println(file.exists());//判断是否存在
      System.out.println(file.length());//获取字节长度
      System.out.println(file.isFile());//是否是文件
      System.out.println(file.canWrite());//获取文件可读/可写属性返回boolean
      System.out.println(file.canRead());//获取文件可读/可写属性返回boolean
      System.out.println(file.isAbsolute());
      System.out.println("文件路径URI:"+file.toURI());
      System.out.println("文件最后修改时间:"+new Date(file.lastModified()));//获取最后修改时间
   }

执行结果:

test.txt
G:\FFOutput\test.txt
G:\FFOutput\test.txt
G:\FFOutput
true
10
true
true
文件路径URI:file:/G:/FFOutput/test.txt
文件路径URI:2020-05-07

二.java IO库 字节流的定义

含有两个抽象父类:

I/O部分内容很庞大涉及广泛:

标准输入/输出、文件操作、网络上的数据流、字符串流、对象流

IO流:

一个流 从源端到目的端 可以是内存,磁盘文件,可以是一个URL,流的方向很重要,根据方向进行传输。用户读取消息 但不能写 。相反,对输出流,只能往流出流,写而不能读。

1.InputStream流:

java 实时输出文件 java输出文件名_java 实时输出文件


2.OutputStream 流:

java 实时输出文件 java输出文件名_子类_02

java流包含字节流,字符流,字节流通过IO设备以字节数据的方式读入,而字符流通过字节流读出数据转化为字符"流"的形式由用户驱使。

对字节流,其流类的层次结构如图20-3和图20-4所示,从图中可以看出,InputStream是所有字节输入流的祖先,而OutputStream是所有字节输出流的祖先。

java 实时输出文件 java输出文件名_子类_03

InputStream类:

它是个抽象类,作为字节输入流的直接或间接的父类

子类包括必须的方法, 读取、标记、复位、关闭等方法 错误抛出IOException异常

int read()读一个字节
int read(btye b[])读多个字节放置b数组中,读取字节数为b的长度返回读取字节的数量
int read(byte b[],int off,int len) 读取len 个字节,放置到以下标off开始的字节数组b中,返回读取字节数
close()流操作完毕后必须关闭

int  available()返回值为六种尚未读出字节的数量
long ship(long n)跳过n个字节不读,返回实际跳过的数量

void mark(int readlimit) 记录当前读指针所在的位置 ,再继续度readlimit个字节失效
void reset() 重新将指针返回mark 指向处
boolean markSupport() 是否支持返回指针处

OutputStream 类:

它是抽象类

外部设备输出数据时抛出IOException异常

void write(int b) 往流中写入一个字节b
void write(byte b[]) 写入字节数组b
void write(byte b[],int off,int len 写入len 个字节,放置到以下标off开始的字节数组b中,返回读取字节数
flush() 把缓存的内容强制输出到流中
close() 完毕后关闭

文件操作

FileInputStream 与FileOutStream 类,第一个类的源端喝第二个目的端都是磁盘文件,他们的构造方法允许通过文件的路径名构造对应的流

FileInputStream infile = new FileInputStream("myfile.dat");
FileOutputStream outfile = new FileOutputStream("results.dat");

文件输入流FileInputStream类

读取需求比较简单,用户可以使用FileInputStream类 从InputStream中派生出

try{
    FileInputStream in = new FileInputStream("G:/FFOutput/test.txt");
    int n = 512; //手动创建的缓冲区 后方用BufferedInputStream代替
    byte buffer[] = new byte[n];
    while((in.read(buffer,0,n)!= -1)&&(n > 0)){
        System.out.println(new String(buffer));
    }
    System.out.println();
    in.close();
}catch (IOException i){
    System.out.print(i);
}catch (Exception e){
    System.out.print(e);
}

本例以FileInputStream的read(buffer)方法,存储在缓冲区buffer中,再将以buffer中的值构造的字符串new String(buffer)显示在屏幕上

文件输出类FileOutputStream类

FileOutStream 提供基本的文件写入能力

FileOutputStream(String name); //构造器使用给定的name 创建一个FileOutputStream对象
FileOutputStream(File file);// 使用file对象创造FileOutStream对象

键盘输入字符到Buffer中再存入磁盘中:

try{
            System.out.print("input what you want to save test:");
            int count,n =512; //手动创建的缓冲区 后方应用BufferedOut代替
            byte[] buffer = new byte[n];
            count = System.in.read(buffer);
            FileOutputStream out = new FileOutputStream("G:/FFOutput/WriteFile.txt");
            out.write(buffer,0,count);
            out.close();
            System.out.println("aleardy save");
      }catch (IOException i){
         System.out.print(i);
      }catch (Exception e){
         System.out.print(e);
      }

缓存输入/输出流

BuffereredInputStream
BufferedOutPutStream

BufferredInputStream是 FileterInputStream的子类 又是InputStream的子类,实际实现缓存出入/输出的功能,避免空间的浪费

try{
            FileInputStream in = new FileInputStream("G:/FFOutput/test.txt");
            FileOutputStream out = new FileOutputStream("G:/FFOutput/a.txt");
            BufferedInputStream bufferin = new BufferedInputStream(in);
            BufferedOutputStream bufferout = new BufferedOutputStream(out);
            byte[] date = new byte[1];
            while(bufferin.read(date)!=-1){
               bufferout.write(date);
            }
            bufferout.flush();
            bufferin.close();
            bufferout.close();
      }catch (ArrayIndexOutOfBoundsException i){
         System.out.print(i);
      }catch (IOException e){
         System.out.print(e);
      }
}

三.java IO库 字符流的定义

字符流主要是用于支持Unicode的文字内容,绝大多数的字节流提供的类,都可以找到对应的类。

reader帮助用户再unicode流中获得字符数据

Writer实现了输出

java 实时输出文件 java输出文件名_java_04

字符文件读写器

FileReader和FileWrite分别是Reader和Writer的子类,构造方法如下:

FileReader(String filename); //路径
FileReader(File f);// 文件
FileWriter(String filename);
FileWriter(File f);

FileReader类

FileReader fr = new FileReader("G:/FFOutput/test.txt");
int c = fr.read();
while(c != -1){
    System.out.print((char)c);
    c = fr.read();
}
fr.close();

FileWrite类

写入文件 ,存在即覆盖,不存在就创建

FileWriter fw  = new FileWriter("student.text");

fw.write("this is my");
fw.write("student.text");
fw.close();

带缓存的字符文件读写器

为了提高读写效率添加的缓冲读写器,实现一行一行读取写入

BufferedReader

声明后调用readline()方法读取数据 读到\n结束完成读取后使用close()关闭

例1 从文件中读取

BufferedReader bfrd = new BufferedReader(new FileReader("student.text"));
      String str = bfrd.readLine();
      while(str != null)
      {
         System.out.print(str+ "\n");
         str = bfrd.readLine();
      }
      bfrd.close();

例2 从用户输入读取

BufferedReader bfrd = new BufferedReader(new InputStreamReader(System.in));
      String a;
      System.out.print("输入:");
      a = bfrd.readLine();
      System.out.print("输入字符为:"+a);
      bfrd.close();

BufferWriter类

BufferedWriter  writer = new BufferedWriter(new FileWriter("student.text"));
      writer.write("this is new string",2, 15);
      writer.newLine();
      writer.write("this is second string");
      writer.close();

读入流,写入流

1.PrintStream类:对于字节流来说

对BufferedOutputStream的替代品

添加输出流添加功能,方面打印各种数值表示形式 在写入字节流时自动调用flush()方法 刷新到文件中4

public PrintStream(OutputStream out)
public PrintStream(OutputStream out,bollean autoFlush)

例:随机数产生 保存

PrintStream ps = new PrintStream("random.txt");
Random r = new Random();
int a;
for(int i =0;i<10;i++){
    a = r.nextInt(100);
    ps.print(a+"\n");
}
ps.close();

2.PrintWriter

向标准输出设备输出流,一次输入一行的操作相较于BufferedWriter

PrintWriter out = new PrintWriter("random.txt");
out.println("hello world");
out.println("mother fucker");
out.close();

四.用户输入

1.使用System.in获取输入

java 标准IO模型中 提供了System.in /out/err 类

一般使用out输出,而 out是一个预先处理过,包装成PrintStream的对象

例:获取用户从键盘的输入:

int a ;
      a = System.in.read();
      System.out.print(a);

2.Scanner

读取用户在命令行输入的值获取不同的类型

nextDouble,nextFloat,nextInt,nextLine,hasDouble,hasInt

构造器:

Scanner read = new Scanner(System.in);

读取用户输入并求平均值

Scanner read = new Scanner(System.in);
      double sum = 0;
      int a = 0;
      while(read.hasNextDouble()){ //判断是否还有double类型的输入
         double x = read.nextDouble();
         sum +=x;
         a++;
      }
      System.out.print(a+"个数的和为"+sum);