📄 readfromfile.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -