java网络编程____简单的字符流操作(reader,writer)
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 {
//bufferReader
InputStream in=new FileInputStream("test.txt");
readStream(in);
//filereader
fileWriteOpe();
}
/**
* @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);
}
}
//