IO流-15

 

 

例:

import java.io.*;
class EncodeStream
{
 public static void main(String[] args)throws IOException
 {
  //writeText();
  
  readText();
 }
 
 public static void readText()throws IOException
 {
  InputStreamReader isr = new InputStreamReader(new FileInputStream("gbk.txt"), "utf-8");
  
  char[] buf = new char[10];//注意:是char型数组
  int len = isr.read(buf);//读数据放到数组中
  
  String str = new String(buf, 0, len);//放到字符中
  
  System.out.println(str);
  
  isr.close(); 
 }
 
 public static void writeText()throws IOException
 {
  OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf.txt"), "utf-8");
  
  osw.write("你好");//写入数据
  
  osw.close();
 }

}

------------------------------------------------------------------------------------------------------------------------------

String ----> byte[]  str.getBytes[];

byte[] ----> String  new String(byte[]);

例:
import java.util.*;
class EncodeDemo
{
 public static void main(String[] args)throws Exception
 {
  String s = "你好";
  
  byte[] b1 = s.getBytes("GBK");//默认是GBK编码
  System.out.println( Arrays.toString(b1) );//把数组变成字符串
  String s1 = new String(b1,"utf-8");//解码
  System.out.println("s1="+s1);//输出
  
  //对s1进行iso8859-1解码
  byte[] b2 = s1.getBytes("utf-8");
  System.out.println(Arrays.toString(b2));
  
  String s2 = new String(b2, "GBK");
  System.out.println("s2="+s2);
  
  //注意,iso8859-1可以这样解码,因为此编码表不识别中文,
  //utf-8不可以这样解码,因为此编码表识别中文
 
 }
}

--------------------------------------------------------------------------------------------

“联通”问题:
class EncodeDemo2
{
 public static void main(String[] args)throws Exception
 {
  String s = "联通";
  
  byte[] by = s.getBytes("gbk");//编码
  
  for(byte b : by) //输出二进制码
  {
   System.out.println(Integer.toBinaryString(b&255));//取二进制后8位
  }
 }
}

-----------------------------------------------------------------------------------------------

个人总结:记住几种常见的编码表,如UTF-8、 GBK、 ISO8859-1等,写入文件通过

转换流OutputStreamWriter和InputStreamReader来实现,注意编码和解码使用的方法

是str.getBytes()和new String(byte[]),"联通"这个问题出现主要是gbk和utf-8之间编码的

区别,具体可查API文档中utf-8的编码形式

 

字符IO可以用于读写文本文件,可以使用以下两个类来实现数据文件读写: 1. FileReader 和 FileWriter:这两个类用于读写文本文件,以字符为单位进行读写。其中,FileReader 用于读取文件内容,FileWriter 用于向文件中写入内容。 2. BufferedReader 和 BufferedWriter:这两个类用于对字符进行缓冲区处理,可以提高读写效率。其中,BufferedReader 用于从字符输入中读取文本,BufferedWriter 用于向字符输出中写入文本。 以下是一个例子,演示如何使用字符IO实现数据文件读写: ```java import java.io.*; public class CharIODemo { public static void main(String[] args) { try { // 创建文件对象 File file = new File("data.txt"); // 写入文件 FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); bw.write("Hello World!"); bw.newLine(); bw.write("Java is awesome."); bw.close(); fw.close(); // 读取文件 FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); fr.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的例子中,我们先创建了一个名为 data.txt 的文件,然后使用 FileWriter 和 BufferedWriter 将数据写入文件中。接着,使用 FileReader 和 BufferedReader 从文件中读取数据,并将读取到的数据输出到控制台上。注意,我们在写入数据时使用了 BufferedWriter 的 newLine() 方法,这是为了在每行数据的结尾添加一个换行符,以便于在读取数据时区分每行数据。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值