输入流问题
package biaodan;
//我想得到输入流的string形式,谁能帮我一下
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import
java.io.FileNotFoundException ;
import
java.io.IOException ;
import java.io.PrintStream;
public class FileInputStreamDemo {
public static void main(String[] args) throws Exception{
String s ="G://username.txt";
StringBuffer strbuf = null;
try
{
FileInputStream fis = new FileInputStream(s);
// BufferedInputStream bis = new BufferedInputStream(fis);
int ch=fis.read();//从输入流中读入下一个字节,字节被装换成0~255之间的一个无符号整数返回
while(ch!=-1)
{
char c;
System.out.print(c=(char)ch);
strbuf.append();//这里不对,我想得到输入流的字符串形式,怎么写?
ch=fis.read();//似乎是read()有参数
}
}catch (
FileNotFoundException e) {System.out.println("文件不存在");}
// System.out.println("the root is: "+strbuf.toString());
// catch (Exception e2) {{System.out.println("输入流异常");}
}
}
------解决方案--------------------
Java code
import java.io.FileInputStream;
import java.io.FileNotFoundException ;
public class FileInputStreamDemo {
public static void main(String[] args) throws Exception {
String s = "G://username.txt";
//String s = "shit";
//StringBuffer strbuf = null;这里不能是null,可以初始化为一个内容是空的StringBuffer,楼主为啥不用StringBuilder?
StringBuffer strbuf = new StringBuffer();
try {
FileInputStream fis = new FileInputStream(s);
// BufferedInputStream bis = new BufferedInputStream(fis);
int ch = fis.read();// 从输入流中读入下一个字节,字节被装换成0~255之间的一个无符号整数返回
while (ch != -1) {
char c;
System.out.print(c = (char) ch);
strbuf.append(c);// 这里不对,我想得到输入流的字符串形式,怎么写?//append是带参数的
ch = fis.read();// 似乎是read()有参数
}
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
}
// System.out.println("the root is: "+strbuf.toString());
// catch (Exception e2) {{System.out.println("输入流异常");}
}
}
------解决方案-------------------- 探讨 Java code import java.io.FileInputStream; import java.io.FileNotFoundException; public class FileInputStreamDemo { public static void main(String[] args) throws Exception { String s = ……