Java第十七天
字节流与字符流转换
字节流转换为字符流(InputStreamReader)
    InputStreamReader是从字节流到字符流的桥:它读取字节,并使用指定的字符集将其解码为字符。
常用构造器
InputStreamReader(InputStream in)
创建一个使用默认字符集的InputStreamReader。
---------------------------------------------------------------------------------
InputStreamReader(InputStream in, Charset cs)
创建一个使用给定字符集的InputStreamReader。
---------------------------------------------------------------------------------
InputStreamReader(InputStream in, CharsetDecoder dec)
创建一个使用给定字符集解码器的InputStreamReader。
---------------------------------------------------------------------------------
InputStreamReader(InputStream in, String charsetName)
创建一个使用命名字符集的InputStreamReader。
---------------------------------------------------------------------------------
使用方法
public class ConverterInDemo {
public static void main(String[] args) {
InputStream in = System.in;
// 若想要使用字符流的高效缓冲区对字节流进行操作则需要转换
// 利用InputStreamReader将字节流转换成字符流
BufferedReader br = new BufferedReader(new InputStreamReader(in));
BufferedWriter bw = null;
String line = null;
try {
bw = new BufferedWriter(new FileWriter("Test.txt"));
while((line = br.readLine()) != null) {
// 当控制台输入exit时,将结束输入
if("exit".equals(line)) {
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
字符流转换成字节流(OutputStreamWriter)
OutputStreamWriter是字符的桥梁流以字节流:向其写入的字符编码成使用指定的字节charset 。
常用构造器
OutputStreamWriter(OutputStream out)
创建一个使用默认字符编码的OutputStreamWriter。
---------------------------------------------------------------------------------
OutputStreamWriter(OutputStream out, Charset cs)
创建一个使用给定字符集的OutputStreamWriter。
---------------------------------------------------------------------------------
OutputStreamWriter(OutputStream out, CharsetEncoder enc)
创建一个使用给定字符集编码器的OutputStreamWriter。
---------------------------------------------------------------------------------
OutputStreamWriter(OutputStream out, String charsetName)
创建一个使用命名字符集的OutputStreamWriter。
---------------------------------------------------------------------------------
使用方法
public class ConverterOutDemo {
public static void main(String[] args) {
InputStream in = System.in;
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader("Test.txt"));
// 创建字符流转换成字节流的对象
bw = new BufferedWriter(new OutputStreamWriter(System.out));
String line = null;
while((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bw != null) {
bw.close();
}
if(br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
打印流
字符打印流(PrintWriter)
特点:
①可以打印各种类型
②封装了字符输出流,可将字节流转换为字符流
③在调用指定方法时会自动刷新(println,printf,或format方法)
④可以直接向文件中写入数据
常用构造器
PrintWriter(File file)
使用指定的文件创建一个新的PrintWriter,而不需要自动的线路刷新。
---------------------------------------------------------------------------------
PrintWriter(File file, String csn)
使用指定的文件和字符集创建一个新的PrintWriter,而不需要自动进行线条刷新。
---------------------------------------------------------------------------------
PrintWriter(OutputStream out)
从现有的OutputStream创建一个新的PrintWriter,而不需要自动线路刷新。
---------------------------------------------------------------------------------
PrintWriter(OutputStream out, boolean autoFlush)
从现有的OutputStream创建一个新的PrintWriter。
---------------------------------------------------------------------------------
PrintWriter(String fileName)
使用指定的文件名创建一个新的PrintWriter,而不需要自动执行行刷新。
---------------------------------------------------------------------------------
PrintWriter(String fileName, String csn)
使用指定的文件名和字符集创建一个新的PrintWriter,而不需要自动线路刷新。
---------------------------------------------------------------------------------
PrintWriter(Writer out)
创建一个新的PrintWriter,没有自动线冲洗。
---------------------------------------------------------------------------------
PrintWriter(Writer out, boolean autoFlush)
创建一个新的PrintWriter。
---------------------------------------------------------------------------------
其方法详见API(PrintWriter)
PrintWriter使用方法
public class PrintWriterDemo {
public static void main(String[] args) {
PrintWriter pw = null;
try {
pw = new PrintWriter("Test.txt");
// 向文件中写入布尔类型的值
pw.print(false);
// 向文件中写入字符类型的值
pw.print('B');
// 向文件中写入字符数组
char[] ch = {'A','B','C','D','E','F','G'};
pw.print(ch);
// 向文件中写入double类型的值
pw.print(3.1415926);
// 向文件中写入float类型的值
pw.print(16.6f);
// 向文件中写入String类型的值
pw.print("测试是否写入成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if(pw != null) {
pw.close();
}
}
}
}
使用打印流将文件内容打印到控制台
public class PrintWriterDemoToConsole {
public static void main(String[] args) {
BufferedReader br = null;
PrintWriter pw = null;
try {
br = new BufferedReader(new FileReader("Test.txt"));
// 当使用此构造器时,不会自动刷新
pw = new PrintWriter(System.out);
// 当使用此构造器时,则会自动刷新
// pw = new PrintWriter(System.out,true);
String line = null;
while((line = br.readLine()) != null) {
pw.print(line);
// 这里是否调用刷新方法取决于上方的构造器
pw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(pw != null) {
pw.close();
}
if(br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用打印流复制文件
public class PrintWriterDemoCopyFile {
public static void main(String[] args) {
BufferedReader br = null;
PrintWriter pw = null;
try {
br = new BufferedReader(new FileReader("Test.txt"));
// 这里使用自动刷新的构造器
pw = new PrintWriter(new FileWriter("AAA.txt"),true);
String line = null;
while((line = br.readLine()) != null) {
pw.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(pw != null) {
pw.close();
}
if(br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字节打印流(PrintStream)
常用构造器
PrintStream(File file)
使用指定的文件创建一个新的打印流,而不需要自动换行。
---------------------------------------------------------------------------------
PrintStream(File file, String csn)
使用指定的文件和字符集创建新的打印流,而不需要自动换行。
---------------------------------------------------------------------------------
PrintStream(OutputStream out)
创建一个新的打印流。
---------------------------------------------------------------------------------
PrintStream(OutputStream out, boolean autoFlush)
创建一个新的打印流。
---------------------------------------------------------------------------------
PrintStream(OutputStream out, boolean autoFlush, String encoding)
创建一个新的打印流。
---------------------------------------------------------------------------------
PrintStream(String fileName)
使用指定的文件名创建新的打印流,无需自动换行。
---------------------------------------------------------------------------------
PrintStream(String fileName, String csn)
创建一个新的打印流,不需要自动换行,具有指定的文件名和字符集。
---------------------------------------------------------------------------------
PrintStream使用方法与PrintWriter一致,区别在于字节与自符
Properties(属性集合类)
定义:Properties类表示一组持久的属性。
特点:
①继承于HashTable,是线程安全的。
②Properties可以保存到流中或从流中加载。
③属性列表中的每个键及其对应的值都是一个字符串(只能保存字符串的键值对)。
常用构造器
Properties()
创建一个没有默认值的空属性列表。
常用方法
Object setProperty(String key, String value)
调用Hashtable方法put。
---------------------------------------------------------------------------------
String getProperty(String key)
使用此属性列表中指定的键搜索属性。
---------------------------------------------------------------------------------
void list(PrintStream out)
将此属性列表打印到指定的输出流。
---------------------------------------------------------------------------------
void list(PrintWriter out)
将此属性列表打印到指定的输出流。
---------------------------------------------------------------------------------
void load(InputStream inStream)
从输入字节流读取属性列表(键和元素对)。
---------------------------------------------------------------------------------
void load(Reader reader)
以简单的线性格式从输入字符流读取属性列表(关键字和元素对)。
---------------------------------------------------------------------------------
void store(OutputStream out, String comments)
将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法加载到 Properties表中的格式输出流。
---------------------------------------------------------------------------------
void store(Writer writer, String comments)
将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式输出到输出字符流。
---------------------------------------------------------------------------------
使用方法(setProperty、getProperty)
public class PropertiesTest {
public static void main(String[] args) {
// 创建Properties对象
Properties prop = new Properties();
// 向Properties集合中添加键值对
prop.setProperty("name", "张三");
prop.setProperty("age", "20");
prop.setProperty("gender", "男");
System.out.println(prop);
// 根据键获取相应的值
String name = prop.getProperty("name");
String age = prop.getProperty("age");
String gender = prop.getProperty("gender");
System.out.println(name+" "+age+" "+gender);
}
}
输出结果:
{age=20, name=张三, gender=男}
张三 20 男
使用方法(load)
public class PropertiesTest2 {
public static void main(String[] args) {
// 创建Properties对象
Properties prop = new Properties();
FileReader fr = null;
try {
fr = new FileReader("Test.txt");
// 从流中将字符流数据读到prop对象中
prop.load(fr);
// 根据键获取相应的值
String name = prop.getProperty("name");
String age = prop.getProperty("age");
String gender = prop.getProperty("gender");
System.out.println(name+" "+age+" "+gender);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
使用方法(store)
public class PropertiesTest3 {
public static void main(String[] args) {
// 创建Properties对象
Properties prop = new Properties();
// 向Properties集合中添加键值对
prop.setProperty("name", "张三");
prop.setProperty("age", "20");
prop.setProperty("gender", "男");
FileWriter fw = null;
try {
fw = new FileWriter("Test1.txt");
// 将prop对象中的键值对输出到字符流中
prop.store(fw, null);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
与流的使用方法(list方法)
public class PropertiesTest {
public static void main(String[] args) {
// 创建Properties对象
Properties prop = new Properties();
// 向Properties集合中添加键值对
prop.setProperty("name", "张三");
prop.setProperty("age", "20");
prop.setProperty("gender", "男");
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileWriter("Test.txt"));
prop.list(pw);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(pw != null) {
pw.close();
}
}
}
}
本文介绍了Java中的字节流与字符流转换,讲解了InputStreamReader和OutputStreamWriter如何进行转换。此外,详细阐述了打印流PrintWriter和PrintStream的使用,包括它们的特点和构造器。最后,探讨了Properties类,它是属性集合类,用于保存和加载键值对,并提供了与流交互的方法。
941

被折叠的 条评论
为什么被折叠?



