readdatafile.java

来自「《jsp网站开发技术》中的源代码(清华大学出版社)」· Java 代码 · 共 71 行

JAVA
71
字号
package readfile;
import java.io.*;
import java.util.*;

public class readDataFile 
{
private String currentRecord = null;
private BufferedReader file;
private String path;
private StringTokenizer token;
//创建文件对象
public readDataFile()
{
file = new BufferedReader(new InputStreamReader(System.in),1);
} 
public readDataFile(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;
} 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?