readfromfile.java
来自「大量jsp编程使用例子」· Java 代码 · 共 68 行
JAVA
68 行
import java.io.*;
import java.util.StringTokenizer;
public class ReadFromFile{
private String currentRecord = null;
private BufferedReader file;
private String path;
private StringTokenizer token;
public ReadFromFile(){
file = new BufferedReader(new InputStreamReader(System.in),1);
}
public ReadFromFile(String filePath) throws FileNotFoundException{
// gets file
path = filePath;
file = new BufferedReader(new FileReader(path));
}
public void setPath(String filePath){
// sets the file
path = filePath;
try {
file = new BufferedReader(new FileReader(path));
} catch (FileNotFoundException e){
System.out.println("file not found");
}
}
public String getPath(){
return path;
} // method getPath
public void fileClose() throws IOException{
// closes file
file.close();
} // method fileClose
public int nextRecord(){
// this method reads the next record and returns the number of tokens or else returns -1
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 double returnDouble(){
// this method returns the next token as a double
double doubleReturn = Double.valueOf(token.nextToken()).doubleValue();
return doubleReturn;
}
public int returnInt(){
// this method returns the next token as an int
int returnint = Integer.parseInt(token.nextToken());
return returnint;
}
public String returnString(){
// this method returns the next token as a String
String stringReturn = token.nextToken();
return stringReturn;
}
public String returnRecord(){
// this method returna the entire record as a string
return currentRecord;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?