超级简单问题?

xxsteven 2003-11-21 10:56:32
初学,想要一个java读一个文件,然后把文件打印出来的离子,要求完整的程序,说明最后有如何读文件的每一行,把每一行的内容放到字符穿里面,
比如:输出如下。
Enter data file: myData.txt
Title: First Data Set
14
12
37
62
...全文
42 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
killme2008 2003-11-22
  • 打赏
  • 举报
回复
执行上面文件
javac cwla
java csla 文件名
文件必须和csla在同一个文件夹,不然你得输入完整路径
xxsteven 2003-11-22
  • 打赏
  • 举报
回复
我try了,不要s=in.readLine();
就可以了谢谢,上面那个问题怎么解决?
killme2008 2003-11-22
  • 打赏
  • 举报
回复
import java.io.*;
class cw1a{
public static void main(String[] args){

try{
BufferedReader in=new BufferedReader(new FileReader(args[0]));
String s,s1;
while( ( s=in.readLine() )!=null)
{
s1+=(s+"\n");

s=in.readLine();
}
System.out.println(s1);
}
catch(Exception e){}


}
}
xxsteven 2003-11-22
  • 打赏
  • 举报
回复
对了,另外我想文件名是程序运行后输入的怎么做呢?都是基础的用c 我会,但我现在不能查java的资料,请再说一下。谢谢
就像:
要求输入文件名:
Enter data file: 1.txt
打印文件:
Title: First Data Set
14
12
37
62
minghui000 2003-11-22
  • 打赏
  • 举报
回复
up
xxsteven 2003-11-22
  • 打赏
  • 举报
回复
谢谢,但现在还有问题,为什么我的文件读出来不对呢?
文件格式:
title
1
2
3
4
但我读出来显示的是
title
2
4
killme2008 2003-11-22
  • 打赏
  • 举报
回复
还有,你自己为什么不去查查API,这些问题看API是能够自己解决的
例如这句话unreported exception java.io.IOException;must be caught or declare
就表明有异常没有处理,那就要去处理啊,这其实你应该会自己调试的
killme2008 2003-11-22
  • 打赏
  • 举报
回复
倒了
又是我错
实在不好意思,没document我不行
import java.io.*;
class cw1a{
public static void main(String[] args){

try{
BufferedReader in=new BufferedReader(new FileReader(args[0]));
String s;
while( ( s=in.readLine() )!=null)
{
System.out.println(s);
s=in.readLine();
}
}
catch(Exception e){}


}
}



xxsteven 2003-11-22
  • 打赏
  • 举报
回复
unreported exception java.io.IOException;must be caught or declare
killme2008 2003-11-22
  • 打赏
  • 举报
回复
出错信息是什么?
xxsteven 2003-11-22
  • 打赏
  • 举报
回复
还是有错,还是3个,在new FileReader(args[0]));,while( ( s=in.readLine() )!=null)
,s=in.readLine();为什么?
killme2008 2003-11-22
  • 打赏
  • 举报
回复
对于filename,好象有个getFileName()方法
你去查查ducument,我记不清
killme2008 2003-11-22
  • 打赏
  • 举报
回复
不好意思是我写错了
BufferedReader in=new BufferedReader(new FileReader(args[0]));
你试试,我现在在外面
import java.io.*;
class cw1a{
public static void main(String[] args){
BufferedReader in=new BufferedReader(new FileReader(args[0]));
String s;
while( ( s=in.readLine() )!=null)
{
System.out.println(s);
s=in.readLine();
}

}
}


killme2008 2003-11-22
  • 打赏
  • 举报
回复
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
class cw1a{
public static void main(String[] args){

MyFrame frame1=new MyFrame();
frame1.setSize(300,200);
frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String filename=new String();
}
class MyFrame extends JFrame
{
JLabel lb=new JLabel("请输入你要打开的文件名:");
JTextField txt=new JTextField(10);
JButton b=new JButton("确定");
public void init()
{
BorderLayout layout=new BorderLayout();
Container cp=getContentPane();
cp.setLayout(layout);
cp.add(layout.NORTH,lb);
cp.add(layout.CENTER,txt);

b.addActionListener(new buttonListener());

cp.add(layout.SOUTH,b);}
}

class buttonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
filename=txt.getText();
BufferedReader in=new BufferedReader(new FileReader(filename));
String s,s1;
while( ( s=in.readLine() )!=null)
{
s1+=(s+"\n");

s=in.readLine();
}
System.out.println(s1);
}
catch(Exception e){}
}
}

}

}
killme2008 2003-11-22
  • 打赏
  • 举报
回复
cp.setLayout(layout)
不用加()了,我写错的,汗
killme2008 2003-11-22
  • 打赏
  • 举报
回复
我写个你看看吧
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
class cw1a{
public static void main(String[] args){

MyFrame frame1=new MyFrame();
frame1.setSize(300,200);
frame1.setVisible(true);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String filename=new String();
}
class buttonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
filename=txt.getText();
BufferedReader in=new BufferedReader(new FileReader(filename));
String s,s1;
while( ( s=in.readLine() )!=null)
{
s1+=(s+"\n");

s=in.readLine();
}
System.out.println(s1);
}
catch(Exception e){}
}
}

}


class MyFrame extends JFrame
{
JLabel lb=new JLabel("请输入你要打开的文件名:");
JTextField txt=new JTextField(10);
JButton b=new JButton("确定");
public void init()
{
BorderLayout layout=new BorderLayout();
Container cp=getContentPane();
cp.setLayout(layout());
cp.add(layout.NORTH,lb);
cp.add(layout.CENTER,txt);

b.addActionListener(new buttonListener());

cp.add(layout.SOUTH,b);}
}
}
我随手写的,在这里没办法试
你试试吧
xxsteven 2003-11-22
  • 打赏
  • 举报
回复
算了,程序坏了,我给分了,
xxsteven 2003-11-22
  • 打赏
  • 举报
回复
msn吧!
xxsteven 2003-11-22
  • 打赏
  • 举报
回复
没有,对不起
killme2008 2003-11-22
  • 打赏
  • 举报
回复
你有没有QQ,这样很痛苦,QQ上说
加载更多回复(6)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧