Java读取txt文件和覆盖写入txt文件和追加写入txt

//创建文件
	public static void createFile(File filename) {		
		try {
			if(!filename.exists()) {
				filename.createNewFile();				
			}
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	//写入txt 内容不被覆盖 追加写入
	public static boolean filechaseWrite(String Content,String filepath) {
		boolean flag=false;	
		try {
				FileWriter fw=new FileWriter(filepath,true);
				fw.write(Content);
				fw.flush();
				fw.close();
				flag=true;
			}catch (Exception e) {
				// 
			 e.printStackTrace();
			}
		return flag;
	}
	
	
	//写入txt内容 覆盖原内容
	public static boolean writetxtfile(String Content,String filepath) {
		boolean flag=false;
		try {
			//写入的txt文档的路径
			PrintWriter pw=new PrintWriter(filepath);
			//写入的内容
			pw.write(Content);
			pw.flush();
			pw.close();
			flag=true;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	//读取txt内容
	public static String readtxtFile(File file) {
		String sResult="";
		try {
			InputStreamReader reader=new InputStreamReader(new FileInputStream(file),"gbk");
			BufferedReader br=new BufferedReader(reader);
			String s=null;
			while((s=br.readLine())!=null) {
				sResult+=s;
				System.out.println(s);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return sResult;
	}

  

猜你喜欢

转载自www.cnblogs.com/yachao1120/p/9300762.html