IO流知识回顾1

5. IO流
1)目的:讲数据以文件或文件夹的形式,永久性的保存在计算机上。
2)目标:放在硬盘上的文件。
3)File类:
4)使用IO流的步骤
a)先定义要使用的文件

        File path=new File("D:"+File.separator+"text");//sdeparator表示斜杠
if(!path.exists()){
path.mkdirs();//不存在创建
}
File file=new File(path,"a1.txt");//文件不存在,输出流自动创建空白文件

// File file1=new File("E:"+File.separator+"text","a1.txt");
// File file2=new File("E:"+File.separator+"text"+File.separator+"a2.txt");
//
// if(!file.exists()){
// try {
// file.createNewFile();//创建空白文件
// } catch (IOException e) {
// e.printStackTrace();
// }
// }


b)选择合适的流
分类:按照数据流的内容分:分为字节流(Stream,多媒体)和字符流(Reader,Writer,文字)
按照数据流的传送方向分:分为输入流(Input,Reader)和输出流(Output,Wirter)
按照数据的级别,分为基础流和包装流
c)进行读写操作
d) 关闭流

//将控制台冲入的文字存放到文件中
Scanner scanner=new Scanner(System.in);
String str=scanner.next();
try {
Writer writer=new FileWriter(file,true);//append是否追加
writer.write(str);
writer.close();//必须关闭
} catch (IOException e) {
e.printStackTrace();
}
        //再从文件中读取数据
// try {
// Reader reader=new FileReader(file);
// int t;
// String s="";
// while ((t=reader.read())!=-1){//读取达到末尾时返回-1
//// int t=reader.read();
// s+=(char)t;//ascii值转字符
// }
// System.out.println(s);
// reader.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }catch (IOException e) {
// e.printStackTrace();
// }

 

注意:a)任何输出流,都是默认写覆盖,实例化输出流的时候,默认把该文件原来的数据都清空
b)如果文件不存在,输出流会自动创建一个空白文件,不会报异常。 但是读操作是,不会创建新文件,会报文件找不到的异常

    public static void main(String[] args) {
//字节流 图片的复制
File path=new File("files");//相对路径
if(!path.exists()){
path.mkdirs();//创建文件
}
File file1=new File(path,"a.jpg");
File file2=new File(path,"b.jpg");
try {
InputStream is=new FileInputStream(file1);
OutputStream os=new FileOutputStream(file2);
int t;
byte []buffer=new byte[1024];//缓冲区
while ((t=is.read(buffer))!=-1){//读取的字符数,阻塞式
// System.out.println(t);
os.write(buffer,0,t);//第0个元素开始读,t读到最后
}
is.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

 

5)缓冲流: BufferedReader,BufferedWriter  

    public static void main(String[] args) {
File path = new File("files");
if(!path.exists()){
path.mkdirs();
}
File file= new File(path,"time.txt");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 EEE HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
String nowtime= formatter.format(now);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String str1 = "",t,lasttime ,str2 ="";
int num;
while((t=br.readLine())!=null&&(lasttime=br.readLine())!=null){
str1 += t; //读写都要自己加"\n"换行
str2 = lasttime;
}
br.close();
if(str1.length()==0&&str2.length()==0){
num=1;
System.out.println("您是第"+num+"次访问!");
}else{
num = Integer.parseInt(str1);
num++;
lasttime=str2;
System.out.println("您是第"+num+"次访问");
System.out.println("您上次访问的时间是:"+lasttime);
System.out.println("您当前的访问时间是:"+nowtime);
}
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(num+"\n");//ASCII
bw.write(nowtime);
bw.flush();//缓冲
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
posted @ 2021-03-11 22:05  YangYuJia  阅读(54)  评论(0)    收藏  举报