java基础学习

您所在的位置:网站首页 java输出流flush java基础学习

java基础学习

2023-03-17 14:20| 来源: 网络整理| 查看: 265

1 文件1.1 文件的创建1.2 文件常用的方法2 IO流2.1 FileInputStream2.2 FileOutputStream2.3 文件的拷贝2.4 FileReaderjava中的char2.5 FileWriter2.6 BufferedReader2.7 BufferedWriter2.9 ObjectOutputStream2.10 ObjectInputStream对象处理流的注意事项2.11 InputStreamReader2.12 OutputStreamReader

1 文件

1.1 文件的创建public void create01() { String filePath = "e:\\news1.txt"; File file = new File(filePath); try { file.createNewFile(); System.out.println("文件创建成功"); } catch (IOException e) { e.printStackTrace(); } }

1.2 文件常用的方法 getName:获取文件名getAbsolutePath:获取文件绝对路径getParent:获取父级目录getlength:文件大小(字节)exists:文件是否存在isFile: 是不是文件isDirectory:是不是目录delet:删除文件mkdir:创建目录mdirs:创建多级目录

2 IO流 抽象基类 字节流 字符流 输入流 InputStream Reader 输出流 OutputStream Writer

这四个类是抽象类,其他IO流对象都是以这四个类派生出来

节点流:直接和数据源相接(File,String,Pipe等)处理流:封装一个节点流,使用了模拟器模式(BufferedReader,BufferedWriter等)

2.1 FileInputStream 从文件读取单个字节 public void readFile01() { String filePath = "e:\\hello.txt"; int readData = 0; FileInputStream fileInputStream = null; try { //创建 FileInputStream 对象,用于读取 文件 fileInputStream = new FileInputStream(filePath); //从该输入流读取一个字节的数据。 如果没有输入可用,此方法将阻止。 //如果返回-1 , 表示读取完毕 while ((readData = fileInputStream.read()) != -1) { System.out.print((char)readData);//转成char显示 } } catch (IOException e) { e.printStackTrace(); } finally { //关闭文件流,释放资源. try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } 用数组读取文件 public void readFile02() { String filePath = "e:\\hello.txt"; //字节数组 byte[] buf = new byte[8]; //一次读取8个字节. int readLen = 0; FileInputStream fileInputStream = null; try { //创建 FileInputStream 对象,用于读取 文件 fileInputStream = new FileInputStream(filePath); //从该输入流读取最多b.length字节的数据到字节数组。 此方法将阻塞,直到某些输入可用。 //如果返回-1 , 表示读取完毕 //如果读取正常, 返回实际读取的字节数 while ((readLen = fileInputStream.read(buf)) != -1) { System.out.print(new String(buf, 0, readLen));//显示 } } catch (IOException e) { e.printStackTrace(); } finally { //关闭文件流,释放资源. try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }

2.2 FileOutputStream new FileOutputStream(filePath) 创建方式,当写入内容是,会覆盖原来的内容new FileOutputStream(filePath, true) 创建方式,当写入内容是,是追加到文件后面 public void writeFile() { //创建 FileOutputStream对象 String filePath = "e:\\a.txt"; FileOutputStream fileOutputStream = null; try { //得到 FileOutputStream对象 对象 //1. new FileOutputStream(filePath) 创建方式,当写入内容是,会覆盖原来的内容 //2. new FileOutputStream(filePath, true) 创建方式,当写入内容是,是追加到文件后面 fileOutputStream = new FileOutputStream(filePath); //写入一个字节 //fileOutputStream.write('H');// //写入字符串 String str = "hello,world!"; //str.getBytes() 可以把 字符串-> 字节数组 //fileOutputStream.write(str.getBytes()); /* write(byte[] b, int off, int len) 将 len字节从位于偏移量 off的指定字节数组写入此文件输出流 */ fileOutputStream.write(str.getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }

2.3 文件的拷贝 创建文件的输入流 , 将文件读入到程序创建文件的输出流, 将读取到的文件数据,写入到指定的文件. public static void main(String[] args) { String srcFilePath = "e:\\Koala.jpg"; String destFilePath = "e:\\Koala3.jpg"; FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(srcFilePath); fileOutputStream = new FileOutputStream(destFilePath); //定义一个字节数组,提高读取效果 byte[] buf = new byte[1024]; int readLen = 0; while ((readLen = fileInputStream.read(buf)) != -1) { //读取到后,就写入到文件 通过 fileOutputStream //即,是一边读,一边写 fileOutputStream.write(buf, 0, readLen);//一定要使用这个方法 } System.out.println("拷贝ok~"); } catch (IOException e) { e.printStackTrace(); } finally { try { //关闭输入流和输出流,释放资源 if (fileInputStream != null) { fileInputStream.close(); } if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }

2.4 FileReader

FileReader以字符为单位读取文件

java中的char

java里的char类型无论何时都是2字节的,字母’A’也不例外,只是在流中就不一定了,因为不同的编码方式有不同的规则。 unicode第一Plane总共有65536个空位,是16位的,占两个字节,因为java仅使用了unicode的第一个Plane,所以也是16位的。 从字符流中以某种字符编码读取读取字符时,会将流中的字节以某种字符编码的机内码规则进行解码,并转换为unicode的值存储到char类型变量中;向字符流以某种字符编码写入字符时,会将char类型的unicode码代表的字符以某种的字符编码的机内码规则进行编码,然后将编码后的字节写入到目标流中。

/** * 单个字符读取文件 */ @Test public void readFile01() { String filePath = "e:\\story.txt"; FileReader fileReader = null; int data = 0; //1. 创建FileReader对象 try { fileReader = new FileReader(filePath); //循环读取 使用read, 单个字符读取 while ((data = fileReader.read()) != -1) { System.out.print((char) data); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileReader != null) { fileReader.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 字符数组读取文件 */ @Test public void readFile02() { System.out.println("~~~readFile02 ~~~"); String filePath = "e:\\story.txt"; FileReader fileReader = null; int readLen = 0; char[] buf = new char[8]; //1. 创建FileReader对象 try { fileReader = new FileReader(filePath); //循环读取 使用read(buf), 返回的是实际读取到的字符数 //如果返回-1, 说明到文件结束 while ((readLen = fileReader.read(buf)) != -1) { System.out.print(new String(buf, 0, readLen)); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileReader != null) { fileReader.close(); } } catch (IOException e) { e.printStackTrace(); } } }

2.5 FileWriterString filePath = "e:\\note.txt"; //创建FileWriter对象 FileWriter fileWriter = null; char[] chars = {'a', 'b', 'c'}; try { fileWriter = new FileWriter(filePath);//默认是覆盖写入// 3) write(int):写入单个字符 fileWriter.write('C');// 4) write(char[]):写入指定数组 fileWriter.write(chars);// 5) write(char[],off,len):写入指定数组的指定部分 fileWriter.write("Curry".toCharArray(), 0, 3);// 6) write(string):写入整个字符串 fileWriter.write(" 你好哦"); fileWriter.write("君子慎独");// 7) write(string,off,len):写入字符串的指定部分 fileWriter.write("上海天津", 0, 2); //在数据量大的情况下,可以使用循环操作. } catch (IOException e) { e.printStackTrace(); } finally { //对应FileWriter , 一定要关闭流,或者flush才能真正的把数据写入到文件 try { //fileWriter.flush(); //关闭文件流,等价 flush() + 关闭 fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("程序结束...");

2.6 BufferedReader

对字节操作使用BufferedInputStream

public static void main(String[] args) throws Exception { String filePath = "f:\\hello.txt"; //创建bufferedReader BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath)); //读取 String line; //按行读取, 效率高 //说明 //1. bufferedReader.readLine() 是按行读取文件 //2. 当返回null 时,表示文件读取完毕 while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } //关闭流, 这里注意,只需要关闭 BufferedReader ,因为底层会自动的去关闭 节点流 bufferedReader.close(); }

2.7 BufferedWriter

对字节操作使用BufferedOutputStream

public static void main(String[] args) throws IOException { String filePath = "f:\\ok.txt"; //创建BufferedWriter //说明: //1. new FileWriter(filePath, true) 表示以追加的方式写入 //2. new FileWriter(filePath) , 表示以覆盖的方式写入 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath)); bufferedWriter.write("你一定能够成为你想要成为的人"); bufferedWriter.newLine();//插入一个和系统相关的换行 bufferedWriter.write("你一定能够成为你想要成为的人"); bufferedWriter.newLine(); bufferedWriter.write("你一定能够成为你想要成为的人"); bufferedWriter.newLine(); //说明:关闭外层流即可 , 传入的 new FileWriter(filePath) ,会在底层关闭 bufferedWriter.close(); }

2.9 ObjectOutputStream

序列化:在保存数据时,保存数据的值和数据类型,要实现可序列化,必须实现Serializable或者Externalizable接口。一般使用Serializable接口(标记化的,里面没有方法)

反序列化:恢复数据时,恢复数据的值和数据类型

对象处理流能够将数据序列化和反序列化

public static void main(String[] args) throws Exception { //序列化后,保存的文件格式,不是纯文本,而是按照他的格式来保存 String filePath = "f:\\data.dat"; ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath)); //序列化数据到 e:\data.dat oos.writeInt(100);// int -> Integer (实现了 Serializable) oos.writeBoolean(true);// boolean -> Boolean (实现了 Serializable) oos.writeChar('a');// char -> Character (实现了 Serializable) oos.writeDouble(9.5);// double -> Double (实现了 Serializable) oos.writeUTF("你一定可以成为你想要成为的人");//String oos.close(); System.out.println("数据保存完毕(序列化形式)"); }

2.10 ObjectInputStreampublic static void main(String[] args) throws IOException, ClassNotFoundException { //指定反序列化的文件 String filePath = "f:\\data.dat"; ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath)); //读取 //1. 读取(反序列化)的顺序需要和你保存数据(序列化)的顺序一致 //2. 否则会出现异常 System.out.println(ois.readInt()); System.out.println(ois.readBoolean()); System.out.println(ois.readChar()); System.out.println(ois.readDouble()); System.out.println(ois.readUTF()); //关闭流, 关闭外层流即可,底层会关闭 FileInputStream 流 ois.close();}

对象处理流的注意事项 读写顺序要一致类中添加serialVersionUID 序列化的版本号,可以提高兼容性序列化对象时,默认将所有的属性都序列化,除了static或transient修饰的成员序列化具有可继承性,其子类默认实现可序列化对象序列化时,对象的属性必须要可序列化

2.11 InputStreamReader

将字节流转为字符流,用InputStreamReader可以指定编码格式,解决乱码的问题

public static void main(String[] args) throws IOException { String filePath = "f:\\a.txt"; //1. 把 FileInputStream 转成 InputStreamReader //2. 指定编码 gbk //InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "gbk"); //3. 把 InputStreamReader 传入 BufferedReader //BufferedReader br = new BufferedReader(isr); //将2 和 3 合在一起 BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(filePath), "gbk")); //4. 读取 String s = br.readLine(); System.out.println("读取内容=" + s); //5. 关闭外层流 br.close(); }

2.12 OutputStreamReaderpublic static void main(String[] args) throws IOException { String filePath = "f:\\hello.txt"; String charSet = "utf-8"; OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath), charSet); osw.write("你好啊,笨蛋"); osw.close(); System.out.println("按照 " + charSet + " 保存文件成功~"); }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3