/* * Created on 2005-3-2 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates *//** * @author xzm * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ import java.io.*; import java.util.*; public class Temp { public static void main(String[] args) { try{
String line;
StringTokenizer st=new StringTokenizer(line);
while(st.hasMoreTokens()){
System.out.println(st.nextToken);
.............
}
* Created on 2005-3-2
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*//**
* @author xzm
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.*;
import java.util.*;
public class Temp { public static void main(String[] args) {
try{
BufferedReader fr=new BufferedReader(new FileReader(new File("tt.tt")));
String temp=fr.readLine();
StringTokenizer st=new StringTokenizer(temp);
while(st.hasMoreTokens()){
int val=Integer.parseInt(st.nextToken());
System.out.println(val);
} }
catch( Exception e)
{
e.printStackTrace();
}
}
}
我来二种方法共大家参考
1.FileReader
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
class c5_01_01 extends JFrame implements ActionListener
{
public static void main(String args[])
{
c5_01_01 frame=new c5_01_01();
}
JButton bu1;
JTextArea ta1;
public c5_01_01()
{
super("haha");
Container c=getContentPane();
c.setLayout(null);
bu1=new JButton("加载文本文件");
bu1.setSize(150,20);
bu1.setLocation(160,20);
bu1.addActionListener(this);
c.add(bu1);
ta1=new JTextArea();
ta1.setSize(420,280);
ta1.setLocation(20,50);
c.add(ta1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,400);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
char buffer[]=new char[2048];
FileReader rd1;
String str1[]={"精彩C++ Builder 6.0程序设计","精彩Flash MX程序设计"};
try
{
rd1=new FileReader("c5_01_01.txt");
//int len1=rd1.read(buffer);
rd1.write(ta1.getText());
rd1.close();
str1=new String(buffer,0,len1);
ta1.setText(str1);
}
catch(IOException e1)
{
return;
}
}
}
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class c5_01_03 extends JFrame implements ActionListener
{
JButton b1;
JTextArea t1;
public static void main(String args[])
{
c5_01_03 frame=new c5_01_03();
}
public c5_01_03()
{
JFrame fr=new JFrame();
Container c = getContentPane();
//按钮
b1=new JButton("打开");
b1.setSize(120,50);
b1.setLocation(30,30);
c.add(b1);
//文本
t1=new JTextArea();
t1.setSize(500,500);
t1.setLocation(50,30);
c.add(t1);
setSize(600,600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
FileInputStream fis1;
String str="";
try
{
fis1=new FileInputStream("a.txt");
byte buffer[]=new byte[fis1.available()];
fis1.read(buffer);
fis1.close();
str=new String(buffer);
t1.setText(str);
}
catch(IOException e1)
{
return;
}
}
}