转:使用StringReader和StringWriter操作字符串

编程举例:写一函数把StringReader中输入流中的字符串全部转换成大写,然后将结果写到一个StringWriter中,然后使用这一函数将以字符串全部转成大写。

 

  1. package cn.itcast.stringstream;  
  2. import java.io.IOException;  
  3. import java.io.StringReader;  
  4. import java.io.StringWriter;  
  5. public class StringStreamTest {  
  6.     public static void main(String[] args) {  
  7.         String str = "abcdefghijklmn";  
  8.         transform(str);  
  9.     }  
  10.     public static void transform(String str) {  
  11.         StringReader sr = new StringReader(str);  
  12.         StringWriter sw = new StringWriter();  
  13.         char[] chars = new char[1024];  
  14.         try {  
  15.             int len = 0;  
  16.             while ((len = sr.read(chars)) != -1) {  
  17.                 String strRead = new String(chars, 0, len).toUpperCase();  
  18.                 System.out.println(strRead);  
  19.                 sw.write(strRead);  
  20.                 sw.flush();  
  21.             }  
  22.             sr.close();  
  23.             sw.close();  
  24.         } catch (IOException e) {  
  25.             e.printStackTrace();  
  26.         } finally {  
  27.             sr.close();  
  28.             try {  
  29.                 sw.close();  
  30.             } catch (IOException e) {  
  31.                 e.printStackTrace();  
  32.             }  
  33.         }  
  34.     }  
  35. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Java中,可以使用JAXB(Java Architecture for XML Binding)来实现XML字符串和Java对象之间的换。 1. 将Java对象换为XML字符串 使用JAXB的Marshaller类可以将Java对象换为XML字符串。以下是一个示例代码: ```java // 创建JAXBContext对象 JAXBContext jaxbContext = JAXBContext.newInstance(Student.class); // 创建Marshaller对象 Marshaller marshaller = jaxbContext.createMarshaller(); // 设置Marshaller的属性 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 将Java对象换为XML字符串 Student student = new Student("Tom", 18); StringWriter writer = new StringWriter(); marshaller.marshal(student, writer); String xmlString = writer.toString(); System.out.println(xmlString); ``` 2. 将XML字符串换为Java对象 使用JAXB的Unmarshaller类可以将XML字符串换为Java对象。以下是一个示例代码: ```java // 创建JAXBContext对象 JAXBContext jaxbContext = JAXBContext.newInstance(Student.class); // 创建Unmarshaller对象 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); // 将XML字符串换为Java对象 String xmlString = "<student><name>Tom</name><age>18</age></student>"; StringReader reader = new StringReader(xmlString); Student student = (Student) unmarshaller.unmarshal(reader); System.out.println(student.getName() + ", " + student.getAge()); ``` 需要注意的是,在将Java对象换为XML字符串和将XML字符串换为Java对象时,都需要先创建JAXBContext对象。JAXBContext是线程安全的,因此建议在应用程序启动时创建一次即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nizhengjia888

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值