如何读取一个txt文件?在线等待 见http://www.chinabs.net/jsp/default.asp?infoid=34 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;public class TestIO{ BufferedReader bw; public static void main(String []args){ try{ File file=new File("G:\\important.txt"); //准备一个文件对象 System.out.println(file.getPath().toString()); //试一下获取路径 bw=new BufferedReader(new FileReader(file)); //用字符流读取文件(用缓冲流封装文件的数据流) while((ss=bw.readLine())!=null){ //循环读取一行,在循环里,每运行一次realLine(),行指针下移一行 System.out.println(ss); sb.append(ss);//把从文件中读出的每行都添到sb里 } bw.close(); System.out.println(sb.toString()); }catch(IOException e){} }} //变量声明 java.lang.String strFileName; //文件名 java.io.File objFile; //文件对象 java.io.FileReader objFileReader; //读文件对象 char[] chrBuffer = new char[10]; //缓冲 int intLength; //实际读出的字符数(一个中文为一个字符) //设置待读文件名 strFileName = "d:\\test.txt"; //创建文件对象 objFile = new java.io.File(strFileName); //判断文件是否存在 if(objFile.exists()){//文件存在 //创建读文件对象 objFileReader = new java.io.FileReader(objFile); //读文件内容 while((intLength=objFileReader.read(chrBuffer))!=-1){ //输出 out.write(chrBuffer,0,intLength); } //关闭读文件对象 objFileReader.close(); } else{//文件不存在 out.println("下列文件不存在:"+strFileName); } java的流 相关类非常庞大InputStreamOutputStreamBufferedInputStreamBufferedOutputStreamDataInputStreamDataOutputStreamReaderWriter对这些对象需要有些认识 给你一个实例吧import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;public class Message extends JFrame { JTextPane t1;JButton ok,cancel; TDialog td=new TDialog(); public Message() { super("Message"); Container con=getContentPane(); con.setLayout(new BorderLayout()); t1=new JTextPane(); ok=new JButton("OK"); cancel=new JButton("Cancel"); con.add(t1); con.add(ok,BorderLayout.NORTH); con.add(cancel,BorderLayout.SOUTH); ok.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser=new JFileChooser(); int state = chooser.showOpenDialog(null); File file = chooser.getSelectedFile(); if(file!=null&&state==JFileChooser.APPROVE_OPTION) { try { FileReader text=new FileReader(file); t2.setText(text.read()); text.close(); } catch(IOException ie){} } } }); cancel.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String args[]) { Message f = new Message(); f.setSize(200,200); f.setVisible(true); f.setBounds(270,200,200,200); }} //路径输入文本文件的位置import java.io.*;public class ch4_9{ public static void main(String args[]) { String temp; File sourceFile,targetFile; BufferedReader source; BufferedWriter target; try { InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader bufin = new BufferedReader(stdin); System.out.print("请输入来源文件路径: "); sourceFile = new File(bufin.readLine()); source = new BufferedReader(new FileReader(sourceFile)); System.out.print("请输入目的文件路径: "); targetFile = new File(bufin.readLine()); target = new BufferedWriter(new FileWriter(targetFile)); System.out.print("确定要复制?(y/n) "); if((bufin.readLine()).equals("y")) { while((temp=source.readLine()) != null) { target.write(temp); target.newLine(); target.flush(); } System.out.println("复制文件完成!!!"); } else { System.out.println("复制文件失败!!!"); return; } stdin.close(); bufin.close(); } catch(IOException E) { System.out.println("发生I/O错误!!!"); } }} jsp文件操作之读取篇 -------------------------------------------------------------------------------- 文件操作是网站编程的重要内容之一,asp关于文件操作讨论的已经很多了,让我们来看看jsp中是如何实现的。 这里用到了两个文件,一个jsp文件一个javabean文件,通过jsp中调用javabean可以轻松读取文本文件,注意请放置一个文本文件afile.txt到web根目录的test目录下,javabean文件编译后将class文件放到对应的class目录下(tomcat环境)。 Read.jsp <html> <head> <title>读取一个文件</title> </head> <body bgcolor="#000000"> <%--调用javabean --%> <jsp:useBean id="reader" class="DelimitedDataFile" scope="request"> <jsp:setProperty name="reader" property="path" value="/test/afile.txt" /> </jsp:useBean> <h3>文件内容:</h3> <p> <% int count = 0; %> <% while (reader.nextRecord() != -1) { %> <% count++; %> <b>第<% out.print(count); %>行:</b> <% out.print(reader.returnRecord()); %><br> <% } %> </p> </body> </html> //DelimitedDataFile.java bean文件源代码 //导入java包 import java.io.*; import java.util.StringTokenizer; public class DelimitedDataFile { private String currentRecord = null; private BufferedReader file; private String path; private StringTokenizer token; //创建文件对象 public DelimitedDataFile() { file = new BufferedReader(new InputStreamReader(System.in),1); } public DelimitedDataFile(String filePath) throws FileNotFoundException { path = filePath; file = new BufferedReader(new FileReader(path)); } //设置文件路径 public void setPath(String filePath) { path = filePath; try { file = new BufferedReader(new FileReader(path)); } catch (FileNotFoundException e) { System.out.println("file not found"); } } //得到文件路径 public String getPath() { return path; } //关闭文件 public void fileClose() throws IOException { file.close(); } //读取下一行记录,若没有则返回-1 public int nextRecord() { int returnInt = -1; try { currentRecord = file.readLine(); } catch (IOException e) { System.out.println("readLine problem, terminating."); } if (currentRecord == null) returnInt = -1; else { token = new StringTokenizer(currentRecord); returnInt = token.countTokens(); } return returnInt; } //以字符串的形式返回整个记录 public String returnRecord() { return currentRecord; } } 发布时间: 2002年07月24日 作 者: coolknight 相关连接 http://www.chinaphp.com/ 在 JDK 1.1中,支持两个新的对象 Reader & Writer, 它们只能用来对文本文件进行操作,而 JDK1.1中的 InputStream & OutputStream 可以对文本文件或二进制文件进行操作。用FileReader 来读取文件的常用方法是:FileReader fr = new FileReader("mydata.txt");BufferedReader br = new BufferedReader(fr); 用 br.readLing() 来读出数据,然后用br.close() 关闭缓存,用fr.close() 关闭文件。 动态代理问题 JAVA\.NET人员 关于多线程的疑问,大家看下 里面的测试小程序为什么会报错:java.lang.IllegalThreadStateException 这个判断语句怎么写? JTextArea和JTextPane的问题 java应用程序的中文乱码问题。 Jbuilder的问题 ******** 最近工作挺闲的,想揽话干 ********* 关于Java树形结构 JAVA新手问一个问题! 高手指点:异步输入输出流的实现问题(关于socket,selector....)
public class TestIO{
BufferedReader bw;
public static void main(String []args){
try{
File file=new File("G:\\important.txt");
//准备一个文件对象
System.out.println(file.getPath().toString());
//试一下获取路径
bw=new BufferedReader(new FileReader(file));
//用字符流读取文件(用缓冲流封装文件的数据流)
while((ss=bw.readLine())!=null){
//循环读取一行,在循环里,每运行一次realLine(),行指针下移一行
System.out.println(ss);
sb.append(ss);//把从文件中读出的每行都添到sb里
}
bw.close();
System.out.println(sb.toString());
}catch(IOException e){}
}
}
java.lang.String strFileName; //文件名
java.io.File objFile; //文件对象
java.io.FileReader objFileReader; //读文件对象
char[] chrBuffer = new char[10]; //缓冲
int intLength; //实际读出的字符数(一个中文为一个字符) //设置待读文件名
strFileName = "d:\\test.txt"; //创建文件对象
objFile = new java.io.File(strFileName); //判断文件是否存在
if(objFile.exists()){//文件存在
//创建读文件对象
objFileReader = new java.io.FileReader(objFile); //读文件内容
while((intLength=objFileReader.read(chrBuffer))!=-1){
//输出
out.write(chrBuffer,0,intLength);
} //关闭读文件对象
objFileReader.close();
}
else{//文件不存在
out.println("下列文件不存在:"+strFileName);
}
InputStream
OutputStreamBufferedInputStream
BufferedOutputStreamDataInputStream
DataOutputStreamReader
Writer
对这些对象需要有些认识
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Message extends JFrame
{
JTextPane t1;JButton ok,cancel;
TDialog td=new TDialog();
public Message()
{
super("Message");
Container con=getContentPane();
con.setLayout(new BorderLayout());
t1=new JTextPane();
ok=new JButton("OK");
cancel=new JButton("Cancel");
con.add(t1);
con.add(ok,BorderLayout.NORTH);
con.add(cancel,BorderLayout.SOUTH);
ok.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser=new JFileChooser();
int state = chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
if(file!=null&&state==JFileChooser.APPROVE_OPTION)
{
try
{
FileReader text=new FileReader(file);
t2.setText(text.read());
text.close();
}
catch(IOException ie){}
}
}
}); cancel.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String args[])
{
Message f = new Message();
f.setSize(200,200);
f.setVisible(true);
f.setBounds(270,200,200,200);
}
}
import java.io.*;public class ch4_9
{
public static void main(String args[])
{
String temp;
File sourceFile,targetFile;
BufferedReader source;
BufferedWriter target;
try
{
InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader bufin = new BufferedReader(stdin);
System.out.print("请输入来源文件路径: ");
sourceFile = new File(bufin.readLine());
source = new BufferedReader(new FileReader(sourceFile));
System.out.print("请输入目的文件路径: ");
targetFile = new File(bufin.readLine());
target = new BufferedWriter(new FileWriter(targetFile));
System.out.print("确定要复制?(y/n) ");
if((bufin.readLine()).equals("y"))
{
while((temp=source.readLine()) != null)
{
target.write(temp);
target.newLine();
target.flush();
}
System.out.println("复制文件完成!!!");
}
else
{
System.out.println("复制文件失败!!!");
return;
}
stdin.close();
bufin.close();
}
catch(IOException E)
{
System.out.println("发生I/O错误!!!");
}
}
}
--------------------------------------------------------------------------------
文件操作是网站编程的重要内容之一,asp关于文件操作讨论的已经很多了,让我们来看看jsp中是如何实现的。
这里用到了两个文件,一个jsp文件一个javabean文件,通过jsp中调用javabean可以轻松读取文本文件,注意请放置一个文本文件afile.txt到web根目录的test目录下,javabean文件编译后将class文件放到对应的class目录下(tomcat环境)。
Read.jsp <html>
<head>
<title>读取一个文件</title>
</head>
<body bgcolor="#000000">
<%--调用javabean --%>
<jsp:useBean id="reader" class="DelimitedDataFile" scope="request">
<jsp:setProperty name="reader" property="path" value="/test/afile.txt" />
</jsp:useBean> <h3>文件内容:</h3> <p> <% int count = 0; %>
<% while (reader.nextRecord() != -1) { %>
<% count++; %>
<b>第<% out.print(count); %>行:</b>
<% out.print(reader.returnRecord()); %><br>
<% } %>
</p>
</body>
</html>
//DelimitedDataFile.java bean文件源代码
//导入java包
import java.io.*;
import java.util.StringTokenizer; public class DelimitedDataFile
{ private String currentRecord = null;
private BufferedReader file;
private String path;
private StringTokenizer token;
//创建文件对象
public DelimitedDataFile()
{
file = new BufferedReader(new InputStreamReader(System.in),1);
}
public DelimitedDataFile(String filePath) throws FileNotFoundException
{
path = filePath;
file = new BufferedReader(new FileReader(path));
}
//设置文件路径
public void setPath(String filePath)
{
path = filePath;
try {
file = new BufferedReader(new
FileReader(path));
} catch (FileNotFoundException e) {
System.out.println("file not found");
}
}
//得到文件路径
public String getPath() {
return path;
}
//关闭文件
public void fileClose() throws IOException
{
file.close();
}
//读取下一行记录,若没有则返回-1
public int nextRecord()
{
int returnInt = -1;
try
{
currentRecord = file.readLine();
}
catch (IOException e)
{
System.out.println("readLine problem, terminating.");
}
if (currentRecord == null)
returnInt = -1;
else
{
token = new StringTokenizer(currentRecord);
returnInt = token.countTokens();
}
return returnInt;
} //以字符串的形式返回整个记录
public String returnRecord()
{ return currentRecord;
}
}
发布时间: 2002年07月24日
作 者: coolknight
相关连接 http://www.chinaphp.com/
JDK1.1中的 InputStream & OutputStream 可以对文本文件或二进制文件进行操作。用FileReader 来读取文件的常用方法是:
FileReader fr = new FileReader("mydata.txt");
BufferedReader br = new BufferedReader(fr);
用 br.readLing() 来读出数据,然后用br.close() 关闭缓存,用fr.close() 关闭文件。