java中io与file的简单了解
2014-11-12 14:53
441 查看
java中输入输出流的简单操作:
package com.ifly.classpractice.day7_30.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.PrintStream; public class StreamDemo { public static void main(String[] args) throws Exception { writeFile1(); // writeFile2(); // readTextFile(); // readTextFile2(); } private static void readTextFile2() throws Exception { FileInputStream in = new FileInputStream("D:/text2.txt"); // System.out.println(in.available()); byte[] buffer = new byte[in.available()]; in.read(buffer); // 将得到的字节数组转化为String System.out.println(new String(buffer)); in.close(); // 关闭输入流 } private static void readTextFile() throws Exception { FileInputStream in = new FileInputStream("D:/text2.txt"); byte[] buffer = new byte[100]; while (in.read(buffer, 0, buffer.length) != -1) { System.out.println(new String(buffer)); buffer = new byte[100]; } in.close(); } private static void writeFile2() throws Exception { // 往文件中追加 FileOutputStream fos = new FileOutputStream("D:/text2.txt", true); PrintStream ps = new PrintStream(fos); for (int i = 0; i < 100; i++) { // ps.println("i have a dream!\r\n"); ps.format("i have %s dream!\r\n", i); ps.write(new String("I love java").getBytes()); } ps.flush(); ps.close(); fos.close(); } private static void writeFile1() throws Exception { FileOutputStream fos = new FileOutputStream(new File("D:/text1.txt")); for (int i = 0; i < 100; i++) { byte[] bytes = String.format("I have %s dream!\r\n", i).getBytes(); fos.write(bytes); } fos.flush(); fos.close(); } }
package com.ifly.classpractice.day7_30.io; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; public class FileReaderWriter { public static void main(String[] args) throws IOException { readerTextFile(); // writeTextFile(); // writeFile1(); // writeFile2(); // readTextFile(); // readTextFile2(); } /** * 用字符方式写文件 * * @throws IOException */ private static void writeTextFile() throws IOException { // FileWriter out = new FileWriter(new File("D:/studentsInfo.txt")); FileWriter out = new FileWriter("D:/studentsInfo.txt", true); // for (int i = 0; i < 10; i++) { // out.write(String.format(" i have %s dream.\r\n", i)); // } out.write("张三,1001,java3班\r\n"); out.write("李四,1002,java3班\r\n"); out.write("王五,1003,java3班\r\n"); out.write("小六,1004,java3班\r\n"); out.close(); } /** * 用字符方式读文件 * * @throws IOException */ private static void readerTextFile() throws IOException { FileReader in = new FileReader(new File("D:/studentsInfo.txt")); int ch = -1; while (-1 != (ch = in.read())) { System.out.print((char) ch); } in.close(); } private static void readTextFile2() throws Exception { FileInputStream in = new FileInputStream("D:/text2.txt"); // System.out.println(in.available()); byte[] buffer = new byte[in.available()]; in.read(buffer); // 将得到的字节数组转化为String System.out.println(new String(buffer)); in.close(); // 关闭输入流 } private static void readTextFile() throws Exception { FileInputStream in = new FileInputStream("D:/text2.txt"); byte[] buffer = new byte[100]; while (in.read(buffer, 0, buffer.length) != -1) { System.out.println(new String(buffer)); buffer = new byte[100]; } in.close(); } private static void writeFile2() throws Exception { FileOutputStream fos = new FileOutputStream("D:/text2.txt", true); PrintStream ps = new PrintStream(fos); for (int i = 0; i < 100; i++) { // ps.println("i have a dream!\r\n"); ps.format("i have %s dream!\r\n", i); ps.write(new String("I love java").getBytes()); } ps.flush(); ps.close(); fos.close(); } private static void writeFile1() throws Exception { FileOutputStream fos = new FileOutputStream(new File("D:/text1.txt")); for (int i = 0; i < 100; i++) { byte[] bytes = String.format("i have %s dream!\r\n", i).getBytes(); fos.write(bytes); } fos.flush(); fos.close(); } private static void viewStudentInfoAll() { FileReader out = null; try { out = new FileReader(new File("D:/studentsResult.txt")); BufferedReader buffer = new BufferedReader(out); String str = new String(); while ((str = buffer.readLine()) != null) { System.out.println(str); } } catch (IOException e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
java中文件路径
package com.ifly.classpractice.day7_30.io; import static java.lang.System.out; import java.io.File; public class FileAndDirectory { public static void main(String[] args) throws Exception { out.println("当前目录"+System.getProperty("user.dir")); File directory = new File(""); out.println(directory.exists()); out.println(directory.getPath()); out.println(directory.getAbsolutePath()); out.println(directory.getCanonicalPath()); File directory0 = new File("../src/com/ifly/classpractice/day7_30/io"); out.println(directory0.exists()); out.println(directory0.getPath()); out.println(directory0.getAbsolutePath()); out.println(directory0.getCanonicalPath()); File directory1 = new File("./src/com/ifly/classpractice/day7_30/io"); out.println(directory1.exists()); out.println(directory1.getPath()); out.println(directory1.getAbsolutePath()); out.println(directory1.getCanonicalPath()); } }
当文件存在时删除,不存在时新建,并重命名文件名称
@Test public void newFileOrDeleteFile() throws IOException { File file = new File("E:/demo.txt"); if (!file.exists()) { file.createNewFile(); } else { file.delete(); } System.out.println(file.exists()); file.renameTo(new File("E:/demo.pdf")); // 重命名 }
拷贝文件(将一个文件复制到另一个文件)
@Test public void testCopyFile() throws Exception { copyFile("E:/d.txt", "E:/e.txt"); // 拷贝文件 } /** * 拷贝文件 * * @param src * @param dest * @throws Exception */ public void copyFile(String src, String dest) throws Exception { FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(dest); FileChannel fcIn = fis.getChannel(); FileChannel fcOut = fos.getChannel(); fcIn.transferTo(0, fcIn.size(), fcOut); fcOut.close(); fcIn.close(); fos.close(); fis.close(); }
列出目录中的所有文件
@Test public void testGetAllFile() throws Exception { getAllFile3("D:/"); // 列出目录中的所有文件 } public void getAllFile3(String string) throws Exception { File[] files = new File(string).listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isFile()) { System.out.println(files[i].getCanonicalPath()); } else { getAllFile3(files[i].getCanonicalPath()); } } }
相关文章推荐
- Java IO操作——简单了解RandomAccessFile随机读写文件操作类的使用
- Mac OS X中报:java.io.UnixFileSystem.createFileExclusively(Native Method)的简单原因
- java.io.File写入写出的一个简单实例
- Mac OS X中报:java.io.UnixFileSystem.createFileExclusively(Native Method)的简单原因
- Java IO-1 File 和 Android 外部存储简单案例
- java.io File操作,流简单说明
- Java IO操作之File类简单操作
- java.io.FileNotFoundException: /tmp/upload_234sad_
- 如何获取文件属性---java.io.File类
- java图像界面开发简单实例-ImageIO、JFileChooser、JMenu、JPanel的应用
- java.io.File 中 getPath(), getAbsolutePath() 和 getCanonicalPath() 三个方法的区别
- java.io.File类
- Log4J配置后, log4j:ERROR setFile(null,true) call failed. 报错:java.io.FileNotFoundException: /log/mytest2.log (系统找不到指定的路径。)
- java.io.file.delete()不能正常删除文件的原因!
- java.io.File 中 getPath(), getAbsolutePath() 和 getCanonicalPath() 三个方法的区别
- 翻译+转:java.io和java.nio性能简单对比
- 翻译+转:java.io和java.nio性能简单对比
- 类 java.io.File
- 彻底明白JAVA的IO系统-1(认识File)
- java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream