JavaIO读写操作 发表于 2017-05-08 | | java 创建文件并写入 123456789101112131415161718192021222324252627282930313233/* * Create by vampire. 2017/5/8 *///This code for creat a file and write it.// First we import java class.import java.io.File;import java.io.IOException;import java.io.PrintWriter;public class DemoJava { public static void main(String[] args){ try {// File file=new File("fileName.txt");//Create .txt file using File class if(!file.exists()){//check fileName.txt whether exist file.createNewFile(); } PrintWriter pw=new PrintWriter(file); pw.println("This is my file content:"); pw.println(10000000); pw.close(); System.out.println("Done successfully!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }} java 读取文件实例 123456789101112131415161718192021222324252627// import java classimport java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public class JavaRead { public static void main(String[] args){ BufferedReader br=null;//initialize try{ br=new BufferedReader(new FileReader("fileName.txt")); String line; while((line=br.readLine())!=null){ System.out.println(line);//read by line } }catch(IOException e){ e.printStackTrace(); }finally{ try { br.close();//close } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }} 您的支持将鼓励我继续向前! 赏 微信打赏 支付宝打赏