如何在JAVA里打开一个.txt 文件 你是要用什么打开???记事本程序吗???还是要用JAVA读出来??? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 FileInputStream file=new FileInputStream("log.txt"); boolean eof=false; int count=0; while(!eof){ int input =file.read(); System.out.println(input+" "); if(input==-1) eof=true; else count++; } file.close(); System.out.println("\n Bytes read:"+count); Runtime runtime= Runtime.getRuntime(); runtime.exec("aa.txt");这个是调可执行的程序的用楼上的代码 二楼的那个东西怎么能读出什么有实际使用价值的东西呢???最基本的也应该写成这样吧???[CODE]import java.io.*;public class TestFileRead{ public static void main(String[] args) throws Exception { FileInputStream in = new FileInputStream("log.txt"); InputStreamReader read = new InputStreamReader(in); BufferedReader reader = new BufferedReader(read); String line; String allContent = ""; while ((line = reader.readLine()) != null) { allContent += line + System.getProperty("line.separator"); //System.out.println(line); } System.out.println(allContent); }}[/CODE] 如果输出的中文字符解码有问题,还可以这么用InputStreamReader read = new InputStreamReader(in,"gb2312");//具体的解码格式可以自己定。还有,如果想要更加人性化,可以写成这样:import java.io.*;public class TestFileRead{ private static String fileName = ""; public static void main(String[] args) throws Exception { fileName = "log.txt"; try { fileName = (args[0].equals(""))? fileName: args[0]; } catch(Exception e) { System.out.println(e.getMessage()); System.out.println("没有输入要打开的文件,默认打开的文件是“" + fileName + "”"); } File file = new File(fileName); FileInputStream in = new FileInputStream(fileName); InputStreamReader read = new InputStreamReader(in); BufferedReader reader = new BufferedReader(read); String line; String allContent = ""; while ((line = reader.readLine()) != null) { allContent += line + System.getProperty("line.separator"); //System.out.println(line); } System.out.println(allContent); }} 呵呵,刚才没注意,有纰漏了。^O^声明"fileName"时,应该这么写:private static String fileName = "log.txt";去掉:fileName = "log.txt";呵呵。 我只是想在按一个按狃的时候打开另一个文件而已 不是在JAVA里要读出文件的内容 File file=new File("c:\\a.txt");FileInputStream in=new FileInputStream(file);//对他的读操作in.read();........FileOutputStream out=new FileOutputStream(file);//写操作out.write()............ 怎么看怎么像《梦里花落知多少》里的“卖女孩儿的小火柴”!!!^O^这样就可以了:public class SxRuntime{ public static void main(String[] args) { try { Runtime.getRuntime().exec("notepad.exe SxRuntime.java"); } catch(Exception e) { System.out.println(e.getMessage()); } }}Runtime runtime= Runtime.getRuntime(); runtime.exec("aa.txt"); 代码复用,工程共享 批量插入问题 关于struts + hibernate 是否有渐进式的选择? corba构件下载 求Web Service资料 crystal report 10 能否部署在weblogic9下面? servlet的web.xml配置问题! hibernate的问题 大家商讨一下这个问题,有关tomcat作服务器,最大访问量是多少 请教如何才可以调用client访问已经部署的ejb组件,谢谢 Tomcat不能启动,不知为什么? eclipse 的 tomcat 插件哪里有下载的????
boolean eof=false;
int count=0;
while(!eof){
int input =file.read();
System.out.println(input+" ");
if(input==-1)
eof=true;
else
count++;
}
file.close();
System.out.println("\n Bytes read:"+count);
runtime.exec("aa.txt");
这个是调可执行的程序的
用楼上的代码
最基本的也应该写成这样吧???
[CODE]
import java.io.*;public class TestFileRead
{
public static void main(String[] args) throws Exception
{
FileInputStream in = new FileInputStream("log.txt");
InputStreamReader read = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(read);
String line;
String allContent = "";
while ((line = reader.readLine()) != null)
{
allContent += line + System.getProperty("line.separator");
//System.out.println(line);
}
System.out.println(allContent);
}
}
[/CODE]
InputStreamReader read = new InputStreamReader(in,"gb2312");//具体的解码格式可以自己定。还有,如果想要更加人性化,可以写成这样:import java.io.*;public class TestFileRead
{
private static String fileName = "";
public static void main(String[] args) throws Exception
{
fileName = "log.txt";
try
{
fileName = (args[0].equals(""))? fileName: args[0];
}
catch(Exception e)
{
System.out.println(e.getMessage());
System.out.println("没有输入要打开的文件,默认打开的文件是“" + fileName + "”");
}
File file = new File(fileName);
FileInputStream in = new FileInputStream(fileName);
InputStreamReader read = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(read);
String line;
String allContent = "";
while ((line = reader.readLine()) != null)
{
allContent += line + System.getProperty("line.separator");
//System.out.println(line);
}
System.out.println(allContent);
}
}
^O^声明"fileName"时,应该这么写:private static String fileName = "log.txt";去掉:fileName = "log.txt";呵呵。
FileInputStream in=new FileInputStream(file);//对他的读操作
in.read();........FileOutputStream out=new FileOutputStream(file);//写操作
out.write()............
^O^这样就可以了:public class SxRuntime
{
public static void main(String[] args)
{
try
{
Runtime.getRuntime().exec("notepad.exe SxRuntime.java");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}Runtime runtime= Runtime.getRuntime();
runtime.exec("aa.txt");