读写文件(普通文件)

public class NormalFile extends File{
       

	public NormalFile(String pathname) {
		super(pathname);
		// TODO Auto-generated constructor stub
	}
	
	void readFile(String pathName) throws IOException{
		
		File file=new File(pathName);
		
			//常用于读取图像,影像和视频之类的文件,输出二进制
//			InputStream inputStream=null;
//			inputStream=new FileInputStream(file);
//			int temptbyte;
//			while((temptbyte=inputStream.read())!=-1){
//				System.out.print(temptbyte);
//			}
//			inputStream.close();
			
			

//		// 常用于读取文本信息,输出字符
//		Reader reader = null;
//		reader = new InputStreamReader(new FileInputStream(file));
//		int tempchar;
//		while ((tempchar = reader.read()) != -1) {
//			if (((char) tempchar) != '\r') {
//				System.out.print((char) tempchar);
//			}
//		}
//		reader.close();

		// 追加内容到文件之后
		String content = "kkkkkkkkkkkkkkkkkkkkkkkkkkkk";
		FileWriter writer = new FileWriter(pathName, true);
		writer.write(content);
		writer.close();
	}
}

public class Test {

	/**
	 * @param args
	 *
	 */
	public static void main(String[] args) {
		String pahtName = "D:\\test.txt";
		NormalFile nfFile = new NormalFile(pahtName);
		if (nfFile.exists()) {
			System.out.print("文件存在");
			try {
				nfFile.readFile(pahtName);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}

你可能感兴趣的:(写文件)