BufferedReader in=new Buffered(new FileReader(args[0]));//args[0]是传入参数
String s,s1;
while((s=in.readLine()!=null)
{
System.out.println(s);
s=in.readLine();
}
String s,s1;
while((s=in.readLine()!=null)
{
System.out.println(s);
s=in.readLine();
}
class cw1a{
public static void main(String[] args){
BufferedReader in=new Buffered(new FileReader(args[0]));
String s;
while( ( s=in.readLine() )!=null)
{
System.out.println(s);
s=in.readLine();
} }
}
BufferedReader in=new Buffered(new FileReader(args[0]));这一行有3个错误,都是格式不能解析为什么?
import java.io.*;
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();
} }
}
你去查查ducument,我记不清
,s=in.readLine();为什么?
又是我错
实在不好意思,没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){}
}
}
例如这句话unreported exception java.io.IOException;must be caught or declare
就表明有异常没有处理,那就要去处理啊,这其实你应该会自己调试的
文件格式:
title
1
2
3
4
但我读出来显示的是
title
2
4
就像:
要求输入文件名:
Enter data file: 1.txt
打印文件:
Title: First Data Set
14
12
37
62
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){}
}
}
就可以了谢谢,上面那个问题怎么解决?
javac cwla
java csla 文件名
文件必须和csla在同一个文件夹,不然你得输入完整路径
那最好做个GUI界面
让用户输入文件名
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);}
}
}
我随手写的,在这里没办法试
你试试吧
不用加()了,我写错的,汗
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){}
}
} } }