作者:Ruby | 来源:互联网 | 2023-10-13 07:36
packagecom.test.net.utils;importjava.io.BufferedReader;importjava.io.FileInputStream;imp
package com.test.net.utils;import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;/*** @author Administrator* 字符流操作*/
public class CharStreamOper {public static void main(String[] args) throws Exception {//bufferReaderInputStream in=new FileInputStream("test.txt");readStream(in);//filereaderfileWriteOpe();}/*** @throws Exception * @读取 request的流 ,ftp通道的流 等流信息*/public static String readStream(InputStream in) throws Exception{// 读取请求内容BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));String line = null;StringBuilder sb = new StringBuilder();while ((line = reader.readLine()) != null) {sb.append(line);}System.out.println(sb.toString());return sb.toString(); }public static void fileWriteOpe() throws Exception {//写入文件FileWriter fileWriter = new FileWriter("test.txt");StringBuilder sbd = new StringBuilder();sbd.append("掌声那单点的的的的单点的\n");sbd.append("2掌声那放到飞凤舞法国恢复\n");sbd.append("3掌声那 撒旦法撒旦法是 旦法是掌声那 撒旦法撒旦法是 旦法是 掌声那 撒旦法撒旦法是 旦法是");fileWriter.write(sbd.toString());if (null != fileWriter) {fileWriter.close();}System.out.print("写入成功....\n");// 读取写入的文件FileReader fileReader = new FileReader("test.txt");int len;StringBuilder sbReader = new StringBuilder();while ((len = fileReader.read()) != -1) {sbReader.append((char)len);}System.out.println("读取文件_______:\n"+sbReader);}
}
//