1.文件

1.1.什么是文件?

答:文件可认为是相关记录或放在一起的数据的集合

1.2.文件- -般存储在哪里?

答: 磁盘,硬盘,文件夹

1.3.JAVA程序如何访向文件属性?

JAVA API:iava.io. File类

2.File类的常用方法

              

 1 /**
2 * 案例1:使用文件操作的9大方法完成文件的判断
3 */
4 import java.io.*;//1.导入接口
5 import java.util.*;
6 public class TestFileMethods {
7 public static void main(String[] args) {
8
9 try {
10 Text();
11 } catch (IOException e) {
12 e.printStackTrace();
13 }
14 }
15 public static void Text() throws IOException {
16 Scanner input=new Scanner(System.in);
17 //2.实例化对象指定的路径
18 File file=new File("D:\\JAVA\\IDEA\\javaS2\\第8章 File类\\hello.txt");//填写路径
19 //3.判断hello.txt路径是否存在,exists()
20 if (file.exists()){
21 if (file.isDirectory()){
22 System.out.println("当前路径是文件夹");
23 }else{
24 System.out.println("当前路径是文件");
25 System.out.println("当前文件存在");
26 System.out.println("文件的相对路径,完整路径:"+file.getPath());
27 System.out.println("文件的名字为:"+file.getName());
28 System.out.println("文件的绝对路径"+file.getAbsolutePath());
29 System.out.println("文件的上一级目录"+file.getParent());
30 System.out.println("文件的长度是:"+file.length());
31 }
32 System.out.print("按1删除文件:");
33 int an=input.nextInt();
34 if (an==1){
35 //删除操作
36 boolean teue=file.delete();
37 if (teue){
38 System.out.println("删除成功");
39 }else {
40 System.out.println("删除失败");
41 }
42 }
43
44 }else {
45 System.out.println("当前文件不存在");
46 //4.创建hello文件
47 boolean bool=file.createNewFile();
48 if (bool){
49 System.out.println("hello文件创建成功");
50 }else {
51 System.out.println("hello文件创建失败");
52 }
53 }
54 }
55 }                                         

3.JAVA的流

理解Java的流和流的分类

3.1.如何读写文件?

答:通过流来读写文件,流是指一连串流动的字符是以先进先出方式发送信息的通道

       

字节流读写文本文件

1.文件的读写

1.1.文本文件的读写

  • 用FileInputStream和FileOutputStream读写文本文件
  • 用BufferedReader和BufferedWriter读写文本文件

1.2.二进制文件的读写

  • 使用DataInputStream和DataOutputStream读写二进制文件

2.字节流

2.1.输入流

基类: InputStream
        子类: FileInputStream
   构造:
         FileInputStream(File file)
        FileInputStream (String name)
  方法:
         int read()            按字节读返回读到的字节
         read (byte[ ] b)    读到字节数组返回读入字节数组的长度
         read (byte[ ] b, int off, int len)   到字节数组返回读入字节数组的长度

 1 /**
2 * 使用字节流来操作文本文件
3 */
4 import java.io.FileInputStream;//1.引入相关类
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8
9 public class FileInputstreamDemo {
10 public static void main(String[] args) {
11 //创建字节输入流
12 InputStream in=null;
13 try {
14 in =new FileInputStream("D:\\hello.txt");
15 int data;//存储读到的字节
16 //实现读取操作
17 while((data=in.read())!=-1){
18 System.out.print((char) data);
19 }
20 } catch (IOException e) {
21 e.printStackTrace();
22 }
23 //关闭流
24 finally {
25 try {
26 if (in!=null){//判断文件是否为空
27 in.close();
28 }
29
30 } catch (IOException e) {
31 e.printStackTrace();
32 }
33 }
34
35 }
36 }
 1 /**
2 * 使用字节流来操作文本文件
3 */
9 public class FileInputstreamDemo {
10 public static void main(String[] args) {
11 //创建字节输入流
12 InputStream in=null;
13 try {
14 in =new FileInputStream("D:\\hello.txt");
15 //使用数组的方式来读取文件,这可以识别中文字符
16 int len;//存取读入数组的长度
17 byte[]words=new byte[1024];
18 while ((len=in.read(words))!=-1){
19 System.out.println(new String(words,0,len));
20 }
21 } catch (IOException e) {
22 e.printStackTrace();
23 }
24 //关闭流
25 finally {
26 try {
27 if (in!=null){//判断文件是否为空
28 in.close();
29 }
30
31 } catch (IOException e) {
32 e.printStackTrace();
33 }
34 }
35
36 }
37 }

3.小节

4.输出流

基类: OutputStream
子类: FileOutputStream
构造:
Fil eOutputStream(File file)
Fil eOutputStream (String name)
Fi 1 eOutputStream (String name, boolean append) true
追加写.
方法:
close ()
flush() 刷新缓冲区
write (byte[] b)
write (byte门b, int off, int len) .
write(int b)

 1     import java.io.File;
2 import java.io.FileNotFoundException;
3 import java.io.FileOutputStream;
4 import java.io.IOException;
5
6 /**
7 * 案例
8 * 将字符串中的信息写出到文本文件
9 */
10 public class FileOutputStreamDemo {
11 public static void main(String[] args) {
12 //1.创建字节输出流
13 FileOutputStream fos=null;
14 try {
15 fos=new FileOutputStream(new File("D:\\学好JAVA.txt"),true);//表示是否可以追加
16 //2.执行写操作
17 String str="HrlloWord学好JAVA";//要写入的字符
18 byte[] by=str.getBytes();//将字符转换为数组
19 fos.write(by,0,by.length);
20 System.out.println("文件更新成功");
21 } catch (FileNotFoundException e) {
22 e.printStackTrace();
23 }catch (IOException e){
24 e.printStackTrace();
25 }
26 finally {
27 //3.关闭流
28 if (fos!=null){
29 try {
30 fos.close();
31 } catch (IOException e) {
32 e.printStackTrace();
33 }
34 }
35 }
36
37
38 }
39 }

4.1.案例:将一个文本中的内容复制到另一个文本中

 1 import java.io.FileInputStream;
2 import java.io.FileOutputStream;
3 import java.io.IOException;
4 import java.nio.charset.StandardCharsets;
5
6 public class Texst {
7 public static void main(String[] args) {
8 FileInputStream inputStream=null;
9 FileOutputStream outputStream=null;
10
11 try {
12 inputStream=new FileInputStream("D:\\学好JAVA.txt");
13 outputStream=new FileOutputStream("D:\\hello.txt",true);
14 int num;
15 byte[]str=new byte[1024];
16 String stow;
17 while ((num=inputStream.read(str))!=-1){
18 System.out.println(stow=new String(str,0,num));
19 byte[]sum=stow.getBytes(StandardCharsets.UTF_8);
20 outputStream.write(sum,0, sum.length);
21 System.out.println("复制成功");
22 }
23 } catch (IOException e) {
24 e.printStackTrace();
25 }
26 }
27 }

小结

字符流和缓冲流读取文本文件

//存取读入数组的长度

------------恢复内容开始------------

1.文件

1.1.什么是文件?

答:文件可认为是相关记录或放在一起的数据的集合

1.2.文件- -般存储在哪里?

答: 磁盘,硬盘,文件夹

1.3.JAVA程序如何访向文件属性?

JAVA API:iava.io. File类

2.File类的常用方法

              

 1 /**
2 * 案例1:使用文件操作的9大方法完成文件的判断
3 */
4 import java.io.*;//1.导入接口
5 import java.util.*;
6 public class TestFileMethods {
7 public static void main(String[] args) {
8
9 try {
10 Text();
11 } catch (IOException e) {
12 e.printStackTrace();
13 }
14 }
15 public static void Text() throws IOException {
16 Scanner input=new Scanner(System.in);
17 //2.实例化对象指定的路径
18 File file=new File("D:\\JAVA\\IDEA\\javaS2\\第8章 File类\\hello.txt");//填写路径
19 //3.判断hello.txt路径是否存在,exists()
20 if (file.exists()){
21 if (file.isDirectory()){
22 System.out.println("当前路径是文件夹");
23 }else{
24 System.out.println("当前路径是文件");
25 System.out.println("当前文件存在");
26 System.out.println("文件的相对路径,完整路径:"+file.getPath());
27 System.out.println("文件的名字为:"+file.getName());
28 System.out.println("文件的绝对路径"+file.getAbsolutePath());
29 System.out.println("文件的上一级目录"+file.getParent());
30 System.out.println("文件的长度是:"+file.length());
31 }
32 System.out.print("按1删除文件:");
33 int an=input.nextInt();
34 if (an==1){
35 //删除操作
36 boolean teue=file.delete();
37 if (teue){
38 System.out.println("删除成功");
39 }else {
40 System.out.println("删除失败");
41 }
42 }
43
44 }else {
45 System.out.println("当前文件不存在");
46 //4.创建hello文件
47 boolean bool=file.createNewFile();
48 if (bool){
49 System.out.println("hello文件创建成功");
50 }else {
51 System.out.println("hello文件创建失败");
52 }
53 }
54 }
55 }                                         

3.JAVA的流

理解Java的流和流的分类

3.1.如何读写文件?

答:通过流来读写文件,流是指一连串流动的字符是以先进先出方式发送信息的通道

       

字节流读写文本文件

1.文件的读写

1.1.文本文件的读写

  • 用FileInputStream和FileOutputStream读写文本文件
  • 用BufferedReader和BufferedWriter读写文本文件

1.2.二进制文件的读写

  • 使用DataInputStream和DataOutputStream读写二进制文件

2.字节流

2.1.输入流

基类: InputStream
        子类: FileInputStream
   构造:
         FileInputStream(File file)
        FileInputStream (String name)
  方法:
         int read()            按字节读返回读到的字节
         read (byte[ ] b)    读到字节数组返回读入字节数组的长度
         read (byte[ ] b, int off, int len)   到字节数组返回读入字节数组的长度

 1 /**
2 * 使用字节流来操作文本文件
3 */
4 import java.io.FileInputStream;//1.引入相关类
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8
9 public class FileInputstreamDemo {
10 public static void main(String[] args) {
11 //创建字节输入流
12 InputStream in=null;
13 try {
14 in =new FileInputStream("D:\\hello.txt");
15 int data;//存储读到的字节
16 //实现读取操作
17 while((data=in.read())!=-1){
18 System.out.print((char) data);
19 }
20 } catch (IOException e) {
21 e.printStackTrace();
22 }
23 //关闭流
24 finally {
25 try {
26 if (in!=null){//判断文件是否为空
27 in.close();
28 }
29
30 } catch (IOException e) {
31 e.printStackTrace();
32 }
33 }
34
35 }
36 }
 1 /**
2 * 使用字节流来操作文本文件
3 */
9 public class FileInputstreamDemo {
10 public static void main(String[] args) {
11 //创建字节输入流
12 InputStream in=null;
13 try {
14 in =new FileInputStream("D:\\hello.txt");
15 //使用数组的方式来读取文件,这可以识别中文字符
16 int len;//存取读入数组的长度
17 byte[]words=new byte[1024];
18 while ((len=in.read(words))!=-1){
19 System.out.println(new String(words,0,len));
20 }
21 } catch (IOException e) {
22 e.printStackTrace();
23 }
24 //关闭流
25 finally {
26 try {
27 if (in!=null){//判断文件是否为空
28 in.close();
29 }
30
31 } catch (IOException e) {
32 e.printStackTrace();
33 }
34 }
35
36 }
37 }

3.小节

4.输出流

基类: OutputStream
子类: FileOutputStream
构造:
Fil eOutputStream(File file)
Fil eOutputStream (String name)
Fi 1 eOutputStream (String name, boolean append) true
追加写.
方法:
close ()
flush() 刷新缓冲区
write (byte[] b)
write (byte门b, int off, int len) .
write(int b)

 1     import java.io.File;
2 import java.io.FileNotFoundException;
3 import java.io.FileOutputStream;
4 import java.io.IOException;
5
6 /**
7 * 案例
8 * 将字符串中的信息写出到文本文件
9 */
10 public class FileOutputStreamDemo {
11 public static void main(String[] args) {
12 //1.创建字节输出流
13 FileOutputStream fos=null;
14 try {
15 fos=new FileOutputStream(new File("D:\\学好JAVA.txt"),true);//表示是否可以追加
16 //2.执行写操作
17 String str="HrlloWord学好JAVA";//要写入的字符
18 byte[] by=str.getBytes();//将字符转换为数组
19 fos.write(by,0,by.length);
20 System.out.println("文件更新成功");
21 } catch (FileNotFoundException e) {
22 e.printStackTrace();
23 }catch (IOException e){
24 e.printStackTrace();
25 }
26 finally {
27 //3.关闭流
28 if (fos!=null){
29 try {
30 fos.close();
31 } catch (IOException e) {
32 e.printStackTrace();
33 }
34 }
35 }
36
37
38 }
39 }

4.1.案例:将一个文本中的内容复制到另一个文本中

 1 import java.io.FileInputStream;
2 import java.io.FileOutputStream;
3 import java.io.IOException;
4 import java.nio.charset.StandardCharsets;
5
6 public class Texst {
7 public static void main(String[] args) {
8 FileInputStream inputStream=null;
9 FileOutputStream outputStream=null;
10
11 try {
12 inputStream=new FileInputStream("D:\\学好JAVA.txt");
13 outputStream=new FileOutputStream("D:\\hello.txt",true);
14 int num;
15 byte[]str=new byte[1024];
16 String stow;
17 while ((num=inputStream.read(str))!=-1){
18 System.out.println(stow=new String(str,0,num));
19 byte[]sum=stow.getBytes(StandardCharsets.UTF_8);
20 outputStream.write(sum,0, sum.length);
21 System.out.println("复制成功");
22 }
23 } catch (IOException e) {
24 e.printStackTrace();
25 }
26 }
27 }

小结

字符流和缓冲流读取文本文件

1.字符编码:

ASCII码           0~127                   8位二进制数1个字节                              16位二进制数(2个字节)                   010101001010                            0^ 65535

Unicode编码格式

字符流
1.输入流
基类: Reader
FileReader
构造:
FileReader (File file)
FileReader (String name)
常用方法:
int read() 读取-一个字符返回字符编码
int read (char[] b)读取到一个字符数组」_
int read (char[] b, int off, int len) 读取到-一个字符数组的某一部分

2.使用FileReader读取文件的步骤

 1 import java.io.FileNotFoundException;
2 import java.io.FilterReader;
3 import java.io.IOException;
4 import java.io.Reader;
5 /**
6 * 使用FileReader和StringBuffer,进行文件读取
7 */
8 public class FileReader {
9 public static void main(String[] args) {
10 Reader reader=null;
11 StringBuilder builder=null;
12 try {
13 //1.创建一个字符流对象
14 reader=new java.io.FileReader("D:\\hello.txt");
15
16 /*int num;//接受读到的字节
17 //2.读取文本文件
18 while ((num= reader.read())!=-1){
19 System.out.print((char) num);//吧读到的字节进行类型转换
20 }*/
21 char[]sum=new char[1024];//存取读到的字节
22 builder=new StringBuilder();//把字节进行重组
23 int word;//接受读到的字节
24 while ((word= reader.read(sum))!=-1){
25 builder.append(sum);
26 System.out.println(builder.toString());
27
}
28
29 } catch (IOException e) {
30 e.printStackTrace();
31 }finally {
32 //3.关闭流
33 if (reader!=null){
34 try {
35 reader.close();
36 } catch (IOException e) {
37 e.printStackTrace();
38 }
39 }
40
41 }
42
43 }
44 }

3.BufferedReader类

3.1. 问题: 如何提高字符流读取文本文件的效率?

使用FileReader类与BufferedReader类

  1. BufferedReader类是Reader类的子类
  2. BufferedReader类带有缓冲区
  3. 按行读取内容的readLine()方法                             BufferedReader类特有的方法

■Reader类常用方法

int read()

int read(char[ ch)
            read(charD c,int off,int len) 
            void close( )
     ■子 类BufferedReader常用的构造方法
             BufferedReader(Reader in)
    ■子 类BufferedReader特有的方法
             readLine()    按行读取

4.使用字符流写文本文件

4.1.输出流

基类: Writer
                  FileWriter
          构造: FileWriter (File file)
                  FIleWriter(File file, boolean append) append表示是否是追加写, true 是
                  FileWriter (String name)
                  FileWriter (String name, boolean append)
         方法:
              close ()
      flush()刷新缓冲区
      write(int c)
                write (char [] ch)
               write(char[], int off, int len)
               write (String str)
     write (String str, int off, int len)

4.2.使用 FileWriter对文本进行写入的操作

 1 import java.io.FileWriter;
2 import java.io.IOException;
3 import java.io.Writer;
4 /**
5 * 使用FileWriterDemo向文本文件中写信息
6 */
7 public class FileWriterDemo {
8 public static void main(String[] args) {
9 //1.创建流
10 Writer writer=null;
11 try {
12 writer=new FileWriter("D:\\我的世界.txt",true);
13 //2.写入信息
14 writer.write("你好呀,塞罗");//将字符输出到指定的流中
15 writer.flush();//刷新输出流
16 System.out.println("添加成功");
17 } catch (IOException e) {
18 e.printStackTrace();
19 }finally {
20 if (writer!=null){
21 try {
22 //3.关闭流
23 writer.close();
24 } catch (IOException e) {
25 e.printStackTrace();
26 }
27 }
28 }
29 }
30 }

4.3.使用BufferedWriter向文本文件中写信息

 1 import java.io.*;
2 import java.io.FileReader;
3
4 /**
5 * 使用BufferedWriter向文本文件中写信息
6 */
7 public class BufferedWriterDemo {
8 public static void main(String[] args) {
9 Writer wr=null;
10 BufferedWriter br=null;
11 Reader re=null;
12 BufferedReader der=null;
13 try {
14 //1.创建流
15 br=new BufferedWriter(wr=new FileWriter("D:\\我是钢铁侠.txt"));
16 //2.写入信息
17 br.write("你哈钢铁侠我是你的粉丝");
18 br.newLine();//换行的意思
19 br.write("你的知识可以传给我吗");
20 br.newLine();
21 br.write("我真的很需要你的知识");
22 br.flush();//一定要记得写缓冲区
23
24 //读取文件中的信息
25 der=new BufferedReader(re=new FileReader("D:\\我是钢铁侠.txt"));
26 String str=null;//接收读取到的信息
27 while ((str=der.readLine())!=null){
28 System.out.println(str);
29 }
30 } catch (IOException e) {
31 e.printStackTrace();
32 }finally {
33 try {
34 //3.关闭流
35 wr.close();
36 } catch (IOException e) {
37 e.printStackTrace();
38 }
39 if (re!=null){
40 try {
41 re.close();
42 } catch (IOException e) {
43 e.printStackTrace();
44 }
45 }
46 }
47
48 }
49 }

                         总结:

                                                          

读写二-进制文件

   1.DataInputStream类
                ■FilterInputStream的子类
                ■与FileInputStream类结合 使用读取二进制文件
   2.DataOutputStream类
     ■FilterOutputStream的子 类
     ■与FileOutputStream类结合使用写二 进制文件

3.使用DataInputStream读二进制文件                                        使用DataOutputStream写=进制文件

         

 1 import java.io.*;
2
3 /**
4 * 使用DataInputStream和DataOutputStream读写文件 一般用于一些二进制文件
5 */
6 public class ReadAndWriteBinaryFile {
7 public static void main(String[] args) {
8 //1.创建流
9 DataInputStream stream=null;
10 DataOutput output=null;
11 try {
12 stream=new DataInputStream(new FileInputStream("D:\\BufferedWriterDemo.class"));//读取文件
13 output=new DataOutputStream(new FileOutputStream("D:\\FileOutputStream.class"));
14 int sum;
15 //2.实现读写操作
16 while ((sum=stream.read())!=-1){
17 output.write(sum);
18 }
19 } catch (IOException e) {
20 e.printStackTrace();
21 }finally {
22 //3.关闭流
23 if (stream!=null){
24 try {
25 stream.close();
26 } catch (IOException e) {
27 e.printStackTrace();
28 }
29 }
30 }
31 }
32 }

序列化和反序列化

1.序列化和反序列化的过程

                             序列化是将对象的状态写入到特定的流中的过程

                          反序列化则是从特定的流中获取数据重新构建对象的过程

     1.1.实现序列化

            

 1 /**
2 * 学生类
3 */
4 public class Student implements java.io.Serializable{
5
6 private static final long serialVersionUID=1l;
7
8 private String name;//姓名
9 private int age;//年龄
10 transient private String gender;//性别
11 public Student(String name,int age,String gender){
12 this.name=name;
13 this.age= age;
14 this.gender=gender;
15 }
16 public String getName() {
17 return name;
18 }
19 public void setName(String name) {
20 this.name = name;
21 }
22 public int getAge() {
23 return age;
24 }
25 public void setAge(int age) {
26 this.age = age;
27 }
28 public String getGender() {
29 return gender;
30 }
31 public void setGender(String gender) {
32 this.gender = gender;
33 }
34 }
 1 import java.io.*;
2
3 /**
4 * 序列化学生对象
5 */
6 public class Serizable0bj {
7 public static void main(String[] args) throws ClassNotFoundException {
8 //1.创建一个需要序列化的学生对象
9 Student dent=new Student("王饱饱",21,"男");
10 //2.创建一个对象流
11 OutputStream os=null;
12 ObjectOutputStream oos=null;
13 //3.创建一个对象输入流
14 FileInputStream is=null;
15 ObjectInputStream ois=null;
16 try {
17 os=new FileOutputStream("D:/studer.bin");
18 oos=new ObjectOutputStream(os);
19 oos.writeObject(dent);
20
21 is=new FileInputStream("D:/studer.bin");
22 ois=new ObjectInputStream(is);
23 Student stu=(Student) ois.readObject();
24 System.out.println("学生姓名:"+stu.getName()+" 年龄:"+stu.getAge()+" 性别:"+stu.getGender());
25
26 } catch (FileNotFoundException e) {
27 e.printStackTrace();
28 }catch (IOException e) {
29 e.printStackTrace();
30 }finally {
31 if (oos!=null){
32 try {
33 //.关闭
34 oos.close();
35 } catch (IOException e) {
36 e.printStackTrace();
37 }
38 }
39 if (is!=null){
40 try {
41 is.close();
42 } catch (IOException e) {
43 e.printStackTrace();
44 }
45 }
46 }
47
48 }
49 }

 

------------恢复内容结束------------

最新文章

  1. 解决Jquery Kendo.xxx is not a function 的方法
  2. 我心中的核心组件(可插拔的AOP)~分布式文件上传组件~基于FastDFS
  3. 浅谈Jquery中的bind(),live(),delegate(),on()绑定事件方式
  4. keil中的存储模式
  5. Mac IDEA快捷键积累
  6. 高级IO复用应用:聊天室程序
  7. ASP.Net MVC @Html类
  8. PHP在浏览器上跟踪调试的方法以及使用ChromePhp、FirePHP的简单介绍
  9. 转:面向切面编程AOP的理解
  10. poj 2271HTML
  11. json解析—Gson以及GsonFormat插件的运用
  12. php留言板
  13. Algorithm --> 判读是否是子树
  14. [Swift]LeetCode987. 二叉树的垂序遍历 | Vertical Order Traversal of a Binary Tree
  15. Oracle 11g设置IP访问限制
  16. Linux-基础学习(五)-mariadb主从复制以及redis学习
  17. web 项目:解决插入 MySQL 数据库时中文乱码问题
  18. c# 获取键盘的输入
  19. Locust 集合点
  20. springboot静态资源处理

热门文章

  1. Python常用功能函数系列总结(一)
  2. 【原创】美团二面:聊聊你对 Kafka Consumer 的架构设计
  3. rocketmq实现延迟队列精确到秒级实现方案2-时间轮和delay-file实现
  4. java原码、反码、补码、位运算
  5. vue学习6-if判断
  6. (1)puppet安装
  7. 005 Linux 命令三剑客之-sed
  8. Kubernetes-Secret
  9. Spring源码-IOC部分-Bean实例化过程【5】
  10. 别人都在认真听课,我埋头写Python为主播疯狂点点点点点赞!