@Test public void test1() throws IOException{ System.out.println("this is SuiteTest1.test1"); // 第1步:使用File类找到一个文件 File f = new File("d:" + File.separator + "test.txt"); // 声明File 对象 // 第2步:通过子类实例化父类对象 OutputStream out = null; String str = "Hello World!!!"; byte b[] = str.getBytes(); // 准备好一个输出的对象 try { out = new FileOutputStream(f); out.write(b); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 第4步:关闭输出流 // out.close(); // 此时没有关闭 }
@Test public void test1() throws IOException{ System.out.println("this is SuiteTest1.test1"); // 第1步:使用File类找到一个文件 File f = new File("d:" + File.separator + "test.txt"); // 声明File 对象 // 第2步:通过子类实例化父类对象 Writer out = null; String str = "Hello World!!!"; // 准备好一个输出的对象 try { out = new FileWriter(f); out.write(str); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 第4步:关闭输出流 // out.close(); // 此时没有关闭 }
@Test public void test1() throws IOException{ System.out.println("this is SuiteTest1.test1"); // 第1步:使用File类找到一个文件 File f = new File("d:" + File.separator + "test.txt"); // 声明File 对象 // 第2步:通过子类实例化父类对象 //OutputStream out = null; Writer out = null; String str = "Hello World!!!"; //byte b[] = str.getBytes(); // 准备好一个输出的对象 try { //out = new FileOutputStream(f); //out.write(b); out = new FileWriter(f); out.write(str); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 第4步:关闭输出流或者flush强制清空缓冲区 out.flush(); //out.close(); }