readfile.java

来自「jsp动态网站开发技术与实践 电子工业出版社 随书附赠源代码」· Java 代码 · 共 75 行

JAVA
75
字号
package com.chapter4;

import java.io.*; 
import java.util.StringTokenizer; 

public class ReadFile 
{ 
	//类成员变量
	private String currentRecord; 
	private BufferedReader file; 
	private String path; 
	private StringTokenizer token;
	
	//建立文件对象
	public int createFileObj(String filePath) 
	{ 
		int i = 0;
		path = filePath; 
		try 
		{ 
			file = new BufferedReader(new FileReader(path)); 
		}
		catch (FileNotFoundException e)
		{ 		
			i = -1;
		} 
		
		return i;
	} 
	
	//得到文件路径 
	public String getPath() 
	{ 
		return path; 
	}		

	//读取下一行记录,若没有则返回-1 
	public int nextRecord() 
	{
		int i = -1; 
		try 
		{ 
			currentRecord = file.readLine(); 
		} 
		catch (IOException e) 
		{ 
			System.out.println("读取文件内容失败!"); 
		} 
		
		if (currentRecord == null) 
		{
			i = -1; 
		}
		else 
		{ 
			token = new StringTokenizer(currentRecord); 
			i = token.countTokens(); 
		} 
		
		return i; 
	} 
	
	//以字符串的形式返回某行数据 
	public String returnRecord() 
	{ 
		return currentRecord; 
	} 
	
	//关闭文件流 
	public void close() throws IOException 
	{ 
		file.close(); 
	} 
} 

⌨️ 快捷键说明

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