站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题java输入输出的初级问题

java输入输出的初级问题

摘要: java输入输出的初级问题


小弟最近初学java I/O 遇到这样一个问题 代码如下:
import java.io.*;
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
class Fin
{
public Fin()
{
try
{
FileInputStream f1=new FileInputStream(-#34;E://项目开发//mycx//IOputTest//1.txt-#34;);
int Reasult;
while((Reasult=f1.read())!=-1)
{
System.out.print((char)Reasult);
}
f1.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
public class InPutTest {

public static void main(String[] args)
{
Fin f=new Fin();
}
}
//////////////////////////////////////
E:/项目开发/mycx/IOputTest/1.txt 的内容是 “123123123123你好”
但是结果输出的是 “123123123123????”
为什么中文部分变成了问号呢?
还有 在“FileInputStream f1=new FileInputStream(-#34;E://项目开发//mycx//IOputTest//1.txt-#34;);”中必须写绝对路径(windows)相对路径出错找不到文件,这又是怎么回事啊?
请问怎么才能输出中文呢? 谢谢


try
{
StringBuffer buffer= new StringBuffer(2048);
String output =-#34;-#34;;
FileInputStream f1=new FileInputStream(-#34;E://项目开发//mycx//IOputTest//1.txt-#34;);
int Reasult;
while((Reasult=f1.read())!=-1)
{
//System.out.print((char)Reasult);
buffer.append((char)Reasult);

}
outpub = new String (buffer.tostring,-#34;gbk-#34;)
f1.close();
}
catch(IOException e)
{
e.printStackTrace();
}



outpub = new String (buffer.tostring(),-#34;gbk-#34;);


一个汉字占两个(char)字节的。而输出是一个字节一个字节的输出,当然不能显示。


outpub = new String (buffer.tostring().getBytes(-#34;ISO8859-1-#34;),-#34;GBK-#34;);


编码或者用字符流 ···


try{
FileReader fr = new FileReader(-#34;D://myjava.txt-#34;);
BufferedReader bw = new BufferedReader(fr);
int Reasult;
try{
aa = bw.readLine();
System.out.print(aa);
}
catch(Exception e)
{ }

while(( aa = bw.readLine())!= null)
{
System.out.print(aa);
}

fr.close();
}
catch(IOException e)
{
e.printStackTrace();
}


问题已解决 谢谢小群和风林火山 两位高手!


↑返回目录
前一篇: resin 设置,开多个端口,对应不同的站点
后一篇: 问个低级又简单的问题?