simplecatch.java

来自「国外的数据结构与算法分析用书」· Java 代码 · 共 34 行

JAVA
34
字号
import java.io.*;

/**	A simple class that reads in its source code and displays it to the
	console.  It will print out error messages if an exception was thrown. */
public class SimpleCatch
{
	/**	Returns the contents of the file whose name is specified by the parameter, or a message
		indicating why it couldn't be completed. */
	public static String readFile(String file)
	{
		try
		{
			FileReader fr = new FileReader(file); // can throw a FileNotFoundException
			BufferedReader br = new BufferedReader(fr);
			String temp, result = new String();
			while ((temp = br.readLine()) != null) // can throw an IOException
				result += temp + "\n";
			br.close();
			return result;
		}catch(FileNotFoundException e)
		{
			return "Can't find the source file to read.";
		}catch(IOException e)
		{
			return "Sorry, a problem occured while reading the file.";
		}
	}

	public static void main(String[] args)
	{
		System.out.println(readFile("SimpleCatch.java"));
	}
}

⌨️ 快捷键说明

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