From JDK API Document: public int read() throws IOExceptionReads a byte of data from this input stream. This method blocks if no input is yet available. Overrides: read in class InputStream Returns: the next byte of data, or -1 if the end of the file is reached. Throws: IOException - if an I/O error occurs. ----------------------------------------------------------------- read return a byte code read from stream. That's why using write(c) can write a correct character to the outputstream. You can use if(c == '\n') to check if the character is a return carrier.
public int read()
throws IOExceptionReads a byte of data from this input stream. This method blocks if no input is yet available.
Overrides:
read in class InputStream
Returns:
the next byte of data, or -1 if the end of the file is reached.
Throws:
IOException - if an I/O error occurs.
-----------------------------------------------------------------
read return a byte code read from stream. That's why using write(c) can write a correct character to the outputstream.
You can use if(c == '\n') to check if the character is a return carrier.
in.read()被定义为int啊,返回的不是读出数据的长度吗??
(in.read()).equals("\n")这个写法不能成立啊,假如强制转换in.read()的类型,那它也只是一个数字字符(比如"8"),永远也不可能等于回车符的,请详细解释.
解决后马上给分
public class TestWrite {
public static void main(String args[]){
try{
File file=new File("1.txt");
FileInputStream in=new FileInputStream(file);
PrintStream out = new PrintStream(new FileOutputStream("2.txt"));
int c;
while((c=in.read())!=-1)
{
out.write(c);
out.flush();
} }catch (IOException e){
System.out.println("IOException");
}
}
}
请问如何判断in.read()读出来的是什么自符??(该问60分)
如果要与‘\n’比较,应该这么写
(in.read()).equals('\\n');
谢谢诸位
public static void main(String args[]){
try{
File file=new File("testwrite1.txt");
FileInputStream in=new FileInputStream(file);
PrintStream out = new PrintStream(new FileOutputStream("testwrite2.txt"));
//File newFile=new File("2.txt"); //不支持中文
//FileWriter out=new FileWriter(newFile); int c;
while((c=in.read())!=-1)
{
System.out.println(c);
if(c==13){
out.print("<br>");
}else{
out.write(c);
out.flush();
}
} }catch (IOException e){
System.out.println("IOException");
}
}
}
你想这样码?
谢谢诸位
值吗??
wrox出版的<java网络编成指南>说in.read()返回读取的长度,莫非真是它错了?
FileOutputStream output = new FileOutputStream("2.txt");
byte[] buffer = new byte[128];
int charsRead;
charsRead = input.read(buffer);
但是你如果用流的方式读,就会返回流的长度了,你自研究一下拉
例:BufferedReader reader=new BufferedReader(new FileReader(filename));
String line="";
String buf="";
while((line=reader.readLine())!=null){
buff+=line+"<br>";
}
.....
然后再写将buf写入文件
哪个流有这种用法啊?
out.write(c)就是写入c对应的字符。这里的c是in.read()从in中依次读出来的,直到文件结尾时返回-1为止,所以可以写入"abc"。
> in.read()被定义为int啊,返回的不是读出数据的长度吗??
返回的不是读出数据的长度, 它返回的是你读出的字节(byte),强制转化为int的。你看的书害人不浅。> (in.read()).equals("\n")这个写法不能成立啊,假如强制转换in.read()的
in.read返回int型,equals是Object的方法,当然不能用于int型。
> 类型,那它也只是一个数字字符(比如"8"),永远也不可能等于回车符的,请详
读入的int的低8位就是ascii码(文本格式的情况),对应的out.write(c)写出的也是c的低8位(就是ascii码),out.write("abc")要看你用的什么流,一般的流是没有这个方法的,你可以用PrintStream。PrintStream写String的一般方法是把String中的Char(16位)提取出来,如果高8位为0,则只输出低8位,否则全部输出。
> 细解释.
> 解决后马上给分
同意:)