JAVA问题:为什么打开文件只打开里面的内容,而不连格式一起打开(如,多行的打开了只有一行))

来源:百度知道 编辑:UC知道 时间:2024/05/12 14:52:59
FileDialog fd = new FileDialog(new Frame(),"打开文件");
fd.setVisible(true);
String fileName = fd.getDirectory()+"\\"+fd.getFile();
try
{
String aLine = null;
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
while((aLine = br.readLine())!=null)
{
ta.append(aLine);
}
br.close();
fr.close();
}
catch (Exception e)
{
System.out.println("Error:"+e);
}

如何写才能连格式也一起打开?请高手提点。

问题在这一行:
while((aLine = br.readLine())!=null)

我懒得去翻书,你自己去翻吧.

你的程序代码是就读了一行亚!
文件都是字节流读取然后再根据需要自己解码
我给你个最简单的读取文本的代码,
你参考一下

public byte[] readFile(String filename)
{
byte[] data = (byte[])null;
try
{
InputStream in = new FileInputStream(filename);
in.read(dataIndex, 0, 5);
int datal=dataIndex[2] * dataIndex[3];
data = new byte[datal];
in.skip(0L);
in.read(data);
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("data="+data.length);
System.out.println("data.toString="+new String( data ));
return data;
}
你参考一下吧!返回的BYTE[] 你可以用NEW STRING( BYTE[] )来得到字符串!