您的位置:首页 > 编程语言 > Java开发

java文件读写的方法整理

2014-09-26 09:33 323 查看

以下代码的注释内容均为正确内容。

package com.mytest;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.Scanner;public class FilenameFilterTest {	/**	 * @param args	 */	public static void main(String[] args) {		// TODO Auto-generated method stub		//		//读文件方法1//			Scanner sc = null;//			try {//				sc = new Scanner(new File("C:/Documents and Settings/new/桌面/笔记.txt") );//				while(sc.hasNextLine())//				{//					System.out.println(sc.nextLine());//				}//			} catch (FileNotFoundException e) {//				// TODO Auto-generated catch block//				e.printStackTrace();//			}finally//			{//				sc.close();//			}				 //读文件方法2//		try {//			BufferedReader br = new BufferedReader(new FileReader("C:/Documents and Settings/new/桌面/笔记.txt"));//			String s1 = "";//			String s2 = "";//			try {//				while((s1 = br.readLine()) != null)//				{//					s2+=s1+"\n";//				}//				br.close();//				System.out.println(s2);//			} catch (IOException e) {//				// TODO Auto-generated catch block//				e.printStackTrace();//			}//			//		} catch (FileNotFoundException e) {//			// TODO Auto-generated catch block//			e.printStackTrace();//		}				//读文件方法3//		try {//			FileReader fr = new FileReader(new File("C:/Documents and Settings/new/桌面/笔记.txt"));//			int ch ;//			try {//				while((ch=fr.read()) != -1)//				{//					System.out.print((char)ch);//				}//			} catch (IOException e) {//				// TODO Auto-generated catch block//				e.printStackTrace();//			}//			//		} catch (FileNotFoundException e) {//			// TODO Auto-generated catch block//			e.printStackTrace();//		}//		//写文件方法1		File file = new File("C:/Documents and Settings/new/桌面/笔记.txt");		FileWriter fw;		try {			//第二个参数为true时,表示追加文件;不写或false时,表示覆盖文件			fw = new FileWriter(file,false);			BufferedWriter bw = new BufferedWriter(fw);			PrintWriter pw = new PrintWriter(bw);		//	pw.append("keyima");			pw.print("这是print");			pw.close();			bw.close();			fw.close();		} catch (IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}			}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: