メインコンテンツ
1 IO ストリームの概要
1.1 IOストリームを学ぶ理由
- データを変数、配列、またはコレクションに保存する
- データはメモリに保存されるため、永続的に保存することはできません。
- コードの実行が完了するとすぐに、すべてのデータが失われます
- IOストリームを使用する
- 1. データをファイルに書き込み、永続的なデータ保存を実現します。
- 2. ファイル内のデータをメモリに読み込みます(Javaプログラム)
1.2 IOストリームとは
- I は入力の略で、データをハードディスクからメモリに入力するプロセスであり、読み取りと呼ばれます。
- O は出力を意味し、メモリからハードディスクへのデータのプロセスです。それを書くと呼ぶ
- IOデータ送信は、フローの方向に応じて、メモリを参照し、読み取りおよび書き込み操作を行う一種のデータフローとみなすことができます。
- 簡単に言うと、メモリは読み取られ、メモリは書き込まれます。
1.3 IOストリームの分類
- 流れの方向に応じて
- 入力ストリーム: データの読み取りに使用されます
- 出力ストリーム: データの書き込みに使用されます
- 種類別
- バイトストリーム
- 文字ストリーム
- 知らせ :
- バイトストリームは任意のファイルを操作できる
- 文字ストリームはプレーン テキスト ファイルでのみ操作できます
- Windows のメモ帳で開いて理解できる場合、そのファイルはプレーン テキスト ファイルです。
2バイトストリーム出力ストリーム
2.1 バイト出力ストリームの使用を開始する
-
FileOutputStream クラス:
OutputStream
多くのサブクラスがあります。最も単純なサブクラスから始めましょう。java.io.FileOutputStream
クラスは、データをファイルに書き出すためのファイル出力ストリームです。
-
施工方法:
public FileOutputStream(File file)
: 指定された File オブジェクトによって表されるファイルに書き込むファイル出力ストリームを作成します。public FileOutputStream(String name)
: 指定された名前のファイルに書き込むファイル出力ストリームを作成します。
public class FileOutputStreamConstructor throws IOException { public static void main(String[] args) { // 使用File对象创建流对象 File file = new File("a.txt"); FileOutputStream fos = new FileOutputStream(file); // 使用文件名称创建流对象 FileOutputStream fos = new FileOutputStream("b.txt"); } }
-
バイト出力ストリームにデータを書き込むためのクイック スタート
- バイト出力ストリーム オブジェクトを作成します。
- データの書き込み
- リソースを解放する
package com.bn.outputstream_demo; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /* 字节输出流写数据快速入门 : 1 创建字节输出流对象。 2 写数据 3 释放资源 */ public class OutputStreamDemo1 { public static void main(String[] args) throws IOException { // 创建字节输出流对象 // 如果指定的文件不存在 , 会自动创建文件 // 如果文件存在 , 会把文件中的内容清空 FileOutputStream fos = new FileOutputStream("day11_demo\\a.txt"); // 写数据 // 写到文件中就是以字节形式存在的 // 只是文件帮我们把字节翻译成了对应的字符 , 方便查看 fos.write(97); fos.write(98); fos.write(99); // 释放资源 // while(true){} // 断开流与文件中间的关系 fos.close(); } }
2.2 バイト出力ストリームへのデータの書き込み方法
-
バイトストリームでデータを書き込む方法
- 1 void write(int b) 一度に 1 バイトのデータを書き込みます
- 2 void write(byte[] b) 一度に 1 バイトの配列データを書き込みます
- 3 void write(byte[] b, int off, int len) バイト配列のデータの一部を一度に書き込みます
package com.bn.outputstream_demo; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /* 字节流写数据的3种方式 1 void write(int b) 一次写一个字节数据 2 void write(byte[] b) 一次写一个字节数组数据 3 void write(byte[] b, int off, int len) 一次写一个字节数组的部分数据 */ public class OutputStreamDemo2 { public static void main(String[] args) throws IOException { // 创建字节输出流对象 FileOutputStream fos = new FileOutputStream("day11_demo\\a.txt"); // 写数据 // 1 void write(int b) 一次写一个字节数据 fos.write(97); fos.write(98); fos.write(99); // 2 void write(byte[] b) 一次写一个字节数组数据 byte[] bys = {65, 66, 67, 68, 69}; fos.write(bys); // 3 void write(byte[] b, int off, int len) 一次写一个字节数组的部分数据 fos.write(bys, 0, 3); // 释放资源 fos.close(); } }
2.3 書き込んだデータの改行・追記
package com.bn.outputstream_demo;
import java.io.FileOutputStream;
import java.io.IOException;
/*
字节流写数据的换行和追加写入
1 字节流写数据如何实现换行呢?
写完数据后,加换行符
windows : \r\n
linux : \n
mac : \r
2 字节流写数据如何实现追加写入呢?
通过构造方法 : public FileOutputStream(String name,boolean append)
创建文件输出流以指定的名称写入文件。如果第二个参数为true ,不会清空文件里面的内容
*/
public class OutputStreamDemo3 {
public static void main(String[] args) throws IOException {
// 创建字节输出流对象
FileOutputStream fos = new FileOutputStream("day11_demo\\a.txt");
// void write(int b) 一次写一个字节数据
fos.write(97);
// 因为字节流无法写入一个字符串 , 把字符串转成字节数组写入
fos.write("\r\n".getBytes());
fos.write(98);
fos.write("\r\n".getBytes());
fos.write(99);
fos.write("\r\n".getBytes());
// 释放资源
fos.close();
}
}
package com.bn.outputstream_demo;
import java.io.FileOutputStream;
import java.io.IOException;
/*
字节流写数据的换行和追加写入
1 字节流写数据如何实现换行呢?
写完数据后,加换行符
windows : \r\n
linux : \n
mac : \r
2 字节流写数据如何实现追加写入呢?
通过构造方法 : public FileOutputStream(String name,boolean append)
创建文件输出流以指定的名称写入文件。如果第二个参数为true ,不会清空文件里面的内容
*/
public class OutputStreamDemo3 {
public static void main(String[] args) throws IOException {
// 创建字节输出流对象
// 追加写数据
// 通过构造方法 : public FileOutputStream(String name,boolean append) : 追加写数据
FileOutputStream fos = new FileOutputStream("day11_demo\\a.txt" , true);
// void write(int b) 一次写一个字节数据
fos.write(97);
// 因为字节流无法写入一个字符串 , 把字符串转成字节数组写入
fos.write("\r\n".getBytes());
fos.write(98);
fos.write("\r\n".getBytes());
fos.write(99);
fos.write("\r\n".getBytes());
// 释放资源
fos.close();
}
// 写完数据换行操作
private static void method1() throws IOException {
// 创建字节输出流对象
FileOutputStream fos = new FileOutputStream("day11_demo\\a.txt");
// void write(int b) 一次写一个字节数据
fos.write(97);
// 因为字节流无法写入一个字符串 , 把字符串转成字节数组写入
fos.write("\r\n".getBytes());
fos.write(98);
fos.write("\r\n".getBytes());
fos.write(99);
fos.write("\r\n".getBytes());
// 释放资源
fos.close();
}
}
3バイトの入力ストリーム
3.1 バイト入力ストリームの概要
-
バイト入力ストリームクラス
- InputStream クラス: バイト入力ストリームの最上位クラス、抽象クラス —
FileInputStream クラス: FileInputStream は、InputStream を継承します。
- InputStream クラス: バイト入力ストリームの最上位クラス、抽象クラス —
-
施工方法
- public FileInputStream(File file) : ファイルタイプのパスからデータを読み取ります
- public FileInputStream(String name) : 文字列パスからデータを読み取ります
-
ステップ
- 入力ストリームオブジェクトを作成する
- 読み取りデータ
- リソースを解放する
-
package com.bn.inputstream_demo; import java.io.FileInputStream; import java.io.IOException; /* 字节输入流写数据快速入门 : 一次读一个字节 第一部分 : 字节输入流类 InputStream类 : 字节输入流最顶层的类 , 抽象类 --- FileInputStream类 : FileInputStream extends InputStream 第二部分 : 构造方法 public FileInputStream(File file) : 从file类型的路径中读取数据 public FileInputStream(String name) : 从字符串路径中读取数据 第三部分 : 字节输入流步骤 1 创建输入流对象 2 读数据 3 释放资源 */ public class FileInputStreamDemo1 { public static void main(String[] args) throws IOException { // 创建字节输入流对象 // 读取的文件必须存在 , 不存在则报错 FileInputStream fis = new FileInputStream("day11_demo\\a.txt"); // 读数据 , 从文件中读到一个字节 // 返回的是一个int类型的字节 // 如果想看字符, 需要强转 int by = fis.read(); System.out.println((char) by); // 释放资源 fis.close(); } }
3.2 複数バイトを読み取るバイト入力ストリーム
package com.bn.inputstream_demo;
import java.io.FileInputStream;
import java.io.IOException;
/*
字节输入流写数据快速入门 : 读多个字节
第一部分 : 字节输入流类
InputStream类 : 字节输入流最顶层的类 , 抽象类
--- FileInputStream类 : FileInputStream extends InputStream
第二部分 : 构造方法
public FileInputStream(File file) : 从file类型的路径中读取数据
public FileInputStream(String name) : 从字符串路径中读取数据
第三部分 : 字节输入流步骤
1 创建输入流对象
2 读数据
3 释放资源
*/
public class FileInputStreamDemo2 {
public static void main(String[] args) throws IOException {
// 创建字节输入流对象
// 读取的文件必须存在 , 不存在则报错
FileInputStream fis = new FileInputStream("day11_demo\\a.txt");
// 读数据 , 从文件中读到一个字节
// 返回的是一个int类型的字节
// 如果想看字符, 需要强转
// int by = fis.read();
// System.out.println(by);
// by = fis.read();
// System.out.println(by);
// by = fis.read();
// System.out.println(by);
//
// by = fis.read();
// System.out.println(by);
// by = fis.read();
// System.out.println(by);
// by = fis.read();
// System.out.println(by);
// 循环改进
int by;// 记录每次读到的字节
while ((by = fis.read()) != -1) {
System.out.print((char) by);
}
// 释放资源
fis.close();
}
}
3.3 画像のコピー
package com.bn.inputstream_demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
需求 : 把 "图片路径\xxx.jpg" 复制到当前模块下
分析:
复制文件,其实就把文件的内容从一个文件中读取出来(数据源),然后写入到另一个文件中(目的地)
数据源:
xxx.jpg --- 读数据 --- FileInputStream
目的地:
模块名称\copy.jpg --- 写数据 --- FileOutputStream
*/
public class FileInputStreamDemo2 {
public static void main(String[] args) throws IOException {
// 创建字节输入流对象
FileInputStream fis = new FileInputStream("D:\\liqin.jpg");
// 创建字节输出流
FileOutputStream fos = new FileOutputStream("day11_demo\\copy.jpg");
// 一次读写一个字节
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// 释放资源
fis.close();
fos.close();
}
}
3.4 例外のキャプチャと処理
-
JDK7以前の処理方法:手動でリソースを解放する
package com.itheima.inputstream_demo; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* 需求 : 对上一个赋值图片的代码进行使用捕获方式处理 */ public class FileInputStreamDemo4 { public static void main(String[] args) { FileInputStream fis = null ; FileOutputStream fos = null; try { // 创建字节输入流对象 fis = new FileInputStream("D:\\liqin.jpg"); // 创建字节输出流 fos = new FileOutpu tStream("day11_demo\\copy.jpg"); // 一次读写一个字节 int by; while ((by = fis.read()) != -1) { fos.write(by); } } catch (IOException e) { e.printStackTrace(); } finally { // 释放资源 if(fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } // 释放资源 if(fos != null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
-
JDK7バージョン最適化処理方法:リソースを自動解放する
-
JDK7 の最適化後は、try-with-resource ステートメントを使用できます。これにより、ステートメントの最後で各リソースが自動的に閉じられます。
簡単な理解: このステートメントを使用すると、リソースは自動的に解放され、最終的にコードブロックを自分で記述する必要はありません。 -
フォーマット :
格式 : try (创建流对象语句1 ; 创建流对象语句2 ...) { // 读写数据 } catch (IOException e) { 处理异常的代码... } 举例 : try ( FileInputStream fis1 = new FileInputStream("day11_demo\\a.txt") ; FileInputStream fis2 = new FileInputStream("day11_demo\\b.txt") ) { // 读写数据 } catch (IOException e) { 处理异常的代码... }
-
-
コードの練習
package com.itheima.inputstream_demo; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* JDK7版本优化处理方式 需求 : 对上一个赋值图片的代码进行使用捕获方式处理 */ public class FileInputStreamDemo5 { public static void main(String[] args) { try ( // 创建字节输入流对象 FileInputStream fis = new FileInputStream("D:\\liqin.jpg"); // 创建字节输出流 FileOutputStream fos = new FileOutputStream("day11_demo\\copy.jpg") ) { // 一次读写一个字节 int by; while ((by = fis.read()) != -1) { fos.write(by); } // 释放资源 , 发现已经灰色 , 提示多余的代码 , 所以使用 try-with-resource 方式会自动关流 // fis.close(); // fos.close(); } catch (IOException e) { e.printStackTrace(); } } }
3.4 バイト入力ストリームは一度に 1 バイトの配列を読み取ります
-
FileInputStream クラス:
- public int read(byte[] b) : 入力ストリームから最大 b.length バイトのデータを読み取り、実際に読み取られたデータの数を返します。
package com.bn.inputstream_demo; import javax.sound.midi.Soundbank; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /* FileInputStream类 : public int read(byte[] b): 1 从输入流读取最多b.length个字节的数据 2 返回的是真实读到的数据个数 */ public class FileInputStreamDemo6 { public static void main(String[] args) throws IOException { // 创建字节输入流对象 FileInputStream fis = new FileInputStream("day11_demo\\a.txt"); // public int read(byte[] b): // 1 从输入流读取最多b.length个字节的数据 // 2 返回的是真实读到的数据个数 byte[] bys = new byte[3]; // int len = fis.read(bys); // System.out.println(len);// 3 // System.out.println(new String(bys));// abc // // len = fis.read(bys); // System.out.println(len);// 2 // System.out.println(new String(bys));// efc System.out.println("==========代码改进==============="); // int len = fis.read(bys); // System.out.println(len);// 3 // System.out.println(new String(bys, 0, len));// abc // // len = fis.read(bys); // System.out.println(len);// 2 // System.out.println(new String(bys, 0, len));// ef System.out.println("==========代码改进==============="); int len; while ((len = fis.read(bys)) != -1) { System.out.print(new String(bys , 0 , len)); } fis.close(); } }
-
一度にバイト配列を読み書きすることで、画像をコピーするコードを改善しました。
package com.bn.inputstream_demo; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /* 需求 : 对复制图片的代码进行使用一次读写一个字节数组的方式进行改进 FileInputStream类 : public int read(byte[] b): 1 从输入流读取最多b.length个字节的数据 2 返回的是真实读到的数据个数 */ public class FileInputStreamDemo7 { public static void main(String[] args) throws IOException { // 创建字节输入流对象 FileInputStream fis = new FileInputStream("D:\\liqin.jpg"); // 创建字节输出流 FileOutputStream fos = new FileOutputStream("day11_demo\\copy.jpg"); byte[] bys = new byte[1024]; int len;// 每次真实读到数据的个数 int by; while ((len = fis.read(bys)) != -1) { fos.write(bys, 0, len); } // 释放资源 fis.close(); fos.close(); } }
4バイトのバッファストリーム
4.1 バイトバッファストリームの概要
-
バイトバッファリングされたストリーム:
- BufferOutputStream: バッファリングされた出力ストリーム
- BufferedInputStream: バッファリングされた入力ストリーム
-
施工方法:
- バイトバッファ出力ストリーム: BufferedOutputStream(OutputStream out)
- バイトバッファ入力ストリーム: BufferedInputStream(InputStream in)
-
構築方法に特定のファイルやパスではなくバイト ストリームが必要なのはなぜですか?
- バイト バッファ ストリームはバッファを提供するだけで読み取りおよび書き込み機能を持たず、実際の読み取りおよび書き込みデータの操作は基本的なバイト ストリーム オブジェクトに依存する必要があります。
4.2 バイトバッファストリームの場合
package com.bn.bufferedstream_demo;
import java.io.*;
/*
字节缓冲流:
BufferOutputStream:缓冲输出流
BufferedInputStream:缓冲输入流
构造方法:
字节缓冲输出流:BufferedOutputStream(OutputStream out)
字节缓冲输入流:BufferedInputStream(InputStream in)
为什么构造方法需要的是字节流,而不是具体的文件或者路径呢?
字节缓冲流仅仅提供缓冲区,不具备读写功能 , 而真正的读写数据还得依靠基本的字节流对象进行操作
需求 : 使用缓冲流进行复制文件
*/
public class BufferedStreamDemo1 {
public static void main(String[] args) throws IOException {
// 创建高效的字节输入流对象
// 在底层会创建一个长度为8192的数组
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\liqin.jpg"));
// 创建高效的字节输出流
// 在底层会创建一个长度为8192的数组
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day11_demo\\copy.jpg"));
// 使用高效流 , 一次读写一个字节
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
}
// byte[] bys = new byte[1024];
// int len;// 每次真实读到数据的个数
// while ((len = bis.read(bys)) != -1) {
// bos.write(bys, 0, len);
// }
// 释放资源
// 在底层会把基本的流进行关闭
bis.close();
bos.close();
}
}
package com.bn.bufferedstream_demo;
import java.awt.image.DataBufferDouble;
import java.io.*;
/*
需求:把“xxx.avi”复制到模块目录下的“copy.avi” , 使用四种复制文件的方式 , 打印所花费的时间
四种方式:
1 基本的字节流一次读写一个字节 : 花费的时间为:196662毫秒
2 基本的字节流一次读写一个字节数组 : 花费的时间为:383毫秒
3 缓冲流一次读写一个字节 : 花费的时间为:365毫秒
4 缓冲流一次读写一个字节数组 : 花费的时间为:108毫秒
分析 :
数据源 : "D:\a.wmv"
目的地 : "day11_demo\copy.wmv"
*/
public class BufferedStreamDemo2 {
public static void main(String[] args) throws IOException {
long startTime = System.currentTimeMillis();
// method1();
// method2();
// method3();
method4();
long endTime = System.currentTimeMillis();
System.out.println("花费的时间为:" + (endTime - startTime) + "毫秒");
}
// 4 缓冲流一次读写一个字节数组
private static void method4() throws IOException {
// 创建高效的字节输入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.wmv"));
// 创建高效的字节输出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day11_demo\\copy.wmv"));
// 一次读写一个字节数组
byte[] bys = new byte[1024];
int len;// 每次真实读到数据的个数
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
// 释放资源
bis.close();
bos.close();
}
// 3 缓冲流一次读写一个字节
private static void method3() throws IOException {
// 创建高效的字节输入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.wmv"));
// 创建高效的字节输出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day11_demo\\copy.wmv"));
// 一次读写一个字节
int by;
while ((by = bis.read()) != -1) {
bos.write(by);
}
// 释放资源
bis.close();
bos.close();
}
// 2 基本的字节流一次读写一个字节数组
private static void method2() throws IOException {
// 创建基本的字节输入流对象
FileInputStream fis = new FileInputStream("D:\\a.wmv");
// 创建基本的字节输出流对象
FileOutputStream fos = new FileOutputStream("day11_demo\\copy.wmv");
// 一次读写一个字节数组
byte[] bys = new byte[1024];
int len;// 每次真实读到数据的个数
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
// 释放资源
fis.close();
fos.close();
}
// 1 基本的字节流一次读写一个字节
private static void method1() throws IOException {
// 创建基本的字节输入流对象
FileInputStream fis = new FileInputStream("D:\\a.wmv");
// 创建基本的字节输出流对象
FileOutputStream fos = new FileOutputStream("day11_demo\\copy.wmv");
// 一次读写一个字节
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// 释放资源
fis.close();
fos.close();
}
}
5.1 プロパティコレクションの概要
-
プロパティはマップ システムのコレクション クラスです。
public class Properties extends Hashtable <Object,Object>
-
IO ストリーム セクションのプロパティを検討する理由
- プロパティにIO関連のメソッドがあります
-
2 列のコレクションとして使用する
- ジェネリックを追加する必要はなく、文字列のみが作業に保存されます。
package com.itheima.properties_demo; import java.util.Map; import java.util.Properties; import java.util.Set; /* 1 properties是一个Map体系的集合类 - `public class Properties extends Hashtable <Object,Object>` 2 为什么在IO流部分学习Properties - Properties中有跟IO相关的方法 3 当做双列集合使用 - 不需要加泛型 , 工作中只存字符串 */ public class PropertiesDemo1 { public static void main(String[] args) { // 创建集合对象 Properties properties = new Properties(); // 添加元素 properties.put("it001" , "张三"); properties.put("it002" , "李四"); properties.put("it003" , "王五"); // 遍历集合 : 键找值 Set<Object> set = properties.keySet(); for (Object key : set) { System.out.println(key + "---" + properties.get(key)); } System.out.println("========================"); // 遍历集合 : 获取对对象集合 , 获取键和值 Set<Map.Entry<Object, Object>> set2 = properties.entrySet(); for (Map.Entry<Object, Object> entry : set2) { Object key = entry.getKey(); Object value = entry.getValue(); System.out.println(key + "---" + value); } } }
5.2 独自の収集方法としてのプロパティ
- Object setProperty(String key, String value) コレクションのキーと値を設定します。どちらも String 型で、put メソッドと同等です。
- String getProperty(String key) このプロパティ リストで指定されたキーを使用してプロパティを検索します。get メソッドと同等です。
- Set stringPropertyNames() は、プロパティ リストから変更不可能なキー セットを返します。キーとそれに対応する値は文字列であり、keySet メソッドと同等です。
package com.itheima.properties_demo;
import java.util.Properties;
import java.util.Set;
/*
Properties作为集合的特有方法
Object setProperty(String key, String value) 设置集合的键和值,都是String类型,相当于put方法
String getProperty(String key) 使用此属性列表中指定的键搜索属性 , 相当于get方法
Set<String> stringPropertyNames() 从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串 , 相当于keySet方法
*/
public class PropertiesDemo2 {
public static void main(String[] args) {
// 创建集合对象
Properties properties = new Properties();
// 添加元素
properties.setProperty("it001", "张三");
properties.setProperty("it002", "李四");
properties.setProperty("it003", "王五");
// 遍历集合 : 键找值
Set<String> set = properties.stringPropertyNames();
for (String key : set) {
System.out.println(key + "---" + properties.getProperty(key));
}
}
}
5.3 プロパティのIO関連メソッド
- void load(InputStream inStream) は、ファイル内のキーと値のペアをバイト ストリームの形式でコレクションに読み取ります。
- void load(Reader リーダー) は、ファイル内のキーと値のペアを文字ストリームの形式でコレクションに読み取ります。
- void store(OutputStream out, String comments) コレクション内のキーと値のペアをバイト ストリームの形式でファイルに書き込みます。2 番目のパラメーターはコメントです。
- void store(Writer Writer, String comments) コレクション内のキーと値のペアを文字ストリームの形式でファイルに書き込みます。2 番目のパラメーターはコメントです。
package com.bn.properties_demo;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/*
Properties和IO流结合的方法
void load(InputStream inStream) 以字节流形式 , 把文件中的键值对, 读取到集合中
//void load(Reader reader) 以字符流形式 , 把文件中的键值对, 读取到集合中
void store(OutputStream out, String comments) 把集合中的键值对,以字节流形式写入文件中 , 参数二为注释
//void store(Writer writer, String comments) 把集合中的键值对,以字符流形式写入文件中 , 参数二为注释
*/
public class PropertiesDemo3 {
public static void main(String[] args) throws IOException {
// 创建Properties集合对象
Properties properties = new Properties();
/w/ void load(InputStream inStream) 以字节流形式 , 把文件中的键值对, 读取到集合中
properties.load(new FileInputStream("day11_demo\\prop.properties"));
// 打印集合中的数据
System.out.println(properties);
}
}
package com.itheima.properties_demo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/*
Properties和IO流结合的方法
void load(InputStream inStream) 以字节流形式 , 把文件中的键值对, 读取到集合中
//void load(Reader reader) 以字符流形式 , 把文件中的键值对, 读取到集合中
void store(OutputStream out, String comments) 把集合中的键值对,以字节流形式写入文件中 , 参数二为注释
//void store(Writer writer, String comments) 把集合中的键值对,以字符流形式写入文件中 , 参数二为注释
*/
public class PropertiesDemo3 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("zhangsan" , "23");
properties.setProperty("lisi" , "24");
properties.setProperty("wangwu" , "25");
properties.store(new FileOutputStream("day11_demo\\prop2.properties") , "userMessage");
}
private static void method1() throws IOException {
// 创建Properties集合对象
Properties properties = new Properties();
// void load(InputStream inStream) 以字节流形式 , 把文件中的键值对, 读取到集合中
properties.load(new FileInputStream("day11_demo\\prop.properties"));
// 打印集合中的数据
System.out.println(properties);
}
}
6 ResourceBundleロードプロパティファイル
学習目標
- ResourceBundle ツール クラスを上手に使用して、プロパティ ファイルの値をすばやく読み取ることができるようになります。
内容説明
【1】APIの紹介
java.util.ResourceBundle
これは抽象クラスであり、そのサブクラス PropertyResourceBundle を使用して、.properties で終わる構成ファイルを読み取ることができます。
静的メソッドを通じてオブジェクトを直接取得します。
static ResourceBundle getBundle(String baseName) 可以根据名字直接获取默认语言环境下的属性资源。
参数注意: baseName
1.属性集名称不含扩展名。
2.属性集文件是在src目录中的
比如:src中存在一个文件 user.properties
ResourceBundle bundle = ResourceBundle.getBundle("user");
ResourceBundle の一般的なメソッド:
String getString(String key) : 通过键,获取对应的值
[2] コードの練習
ResourceBundle ツール クラスを介して
プロパティ ファイルを src ディレクトリに配置し、ResourceBundle を使用してキーと値のペアのデータを取得します。
package com.itheima.resourcebundle_demo;
import java.util.ResourceBundle;
/*
1 java.util.ResourceBundle : 它是一个抽象类
我们可以使用它的子类PropertyResourceBundle来读取以.properties结尾的配置文件
2 static ResourceBundle getBundle(String baseName) 可以根据名字直接获取默认语言环境下的属性资源。
参数注意: baseName
1.属性集名称不含扩展名。
2.属性集文件是在src目录中的
比如:src中存在一个文件 user.properties
ResourceBundle bundle = ResourceBundle.getBundle("user");
3 ResourceBundle中常用方法:
String getString(String key) : 通过键,获取对应的值
优点 : 快速读取属性文件的值
需求 :
通过ResourceBundle工具类
将一个属性文件 放到src目录中,使用ResourceBundle去获取键值对数据
*/
public class ResourceBundleDemo {
public static void main(String[] args) {
// public static final ResourceBundle getBundle(String baseName)
// baseName : properties文件的名字 , 注意 : 扩展名不需要加上 , properties必须在src的根目录下
ResourceBundle resourceBundle = ResourceBundle.getBundle("user");
// String getString(String key) : 通过键,获取对应的值
String value1 = resourceBundle.getString("username");
String value2 = resourceBundle.getString("password");
System.out.println(value1);
System.out.println(value2);
}
}
内容概要
-
ResourceBundle を使用してプロパティ ファイルをロードする場合、プロパティ ファイルをどこに配置する必要がありますか?
src的根目录
-
ResourceBundle を使用して属性値を取得する一般的な手順を説明してください。
1 获取ResourceBundle对象 2 通过ResourceBundle类中的getString(key) : 根据键找值