java学习_File简单的txt读取写入

File  file=new File("newhello.txt");
		if(file.exists()&&file.isFile())
		{
			try {
				//创建文件输入流
				FileInputStream fis=new FileInputStream(file);
				//文件输入流读取
				InputStreamReader isr=new InputStreamReader(fis);
				//创建带有缓冲的读取流
				BufferedReader br=new BufferedReader(isr);
				String lineString;
				while ((lineString=br.readLine())!=null) {
					System.out.println(lineString);
				}
				System.out.println("读取完成");
				br.close();
				isr.close();
				fis.close();
				
				File  file2=new File("newhello2.txt");
				FileOutputStream fos=new FileOutputStream(file2);
				OutputStreamWriter osWriter=new OutputStreamWriter(fos);
				BufferedWriter bw=new BufferedWriter(osWriter);
				bw.write("你麻痹啊\n");
				bw.write("你2麻痹啊\n");
				bw.write("你3麻痹啊\n");
				bw.close();
				osWriter.close();
				fos.close();
				
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

  

posted @ 2015-10-29 14:19  陌路黄泉  阅读(251)  评论(0编辑  收藏  举报