`
lxlyynnah
  • 浏览: 11059 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

java 写入读取文件

    博客分类:
  • java
阅读更多
//文件写入 
	public static void writeFile(){
		 FileWriter fileWriter = null;
		try {
                                               //写入文件地址
			fileWriter = new FileWriter("E:\\Result.txt");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		   int [] a=new int[]{11112,222,333,444,555,666};
		   for (int i = 0; i < a.length; i++) {
		    try {
                                      //写入
				fileWriter.write(String.valueOf(a[i])+" ");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		     }
		   try {
			fileWriter.flush();
			fileWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }


		 private static String readerFile(){
			//读取文件
			String s, s2 = "";
			  try {
			   BufferedReader in = new BufferedReader(new FileReader
			     ("E:/fe.txt"));
			    while ((s = in.readLine()) != null) {
			     s2 += s + "\n";
			    }
			    in.close();
			  } catch (FileNotFoundException e) {
			   e.printStackTrace();
			  } catch (IOException e) {
				e.printStackTrace();
			}
			  System.out.println(s2);
			  return s2;
		 }
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics