以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 Java/Eclipse 』  (http://bbs.xml.org.cn/list.asp?boardid=41)
----  [转帖]java中对于文件属性的一些操作  (http://bbs.xml.org.cn/dispbbs.asp?boardid=41&rootid=&id=26175)


--  作者:binaryluo
--  发布时间:1/5/2006 11:03:00 PM

--  [转帖]java中对于文件属性的一些操作
1. 当Java.io中,如果文件的操作的时候,判断是否隐藏用File.ishiden()


判断是否只读,可用File.canWrite().

      2. 当要设置是否是可读或者是隐藏时,在java中除了提供File.setReadOnly()外,就无其他方法了。

          所以我们必须到Dos环境下去设置,在java中用Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " +R")该方法可以实现。因为路径file.getAbsolutePath()中可能会还有空格,所以必须用引号把它括起来,当作一个参数。这样就可以实现了

(1)   设置只读Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " +R");

(2)   设置可写Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " -R");

(3)   设置隐藏Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " +H");

(4)   设置非隐藏Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " -H");

   3.对于读写文件的一些操作源代码

      package fileControl;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.RandomAccessFile;

public class FileControl {

            private static String fileName;

            private static File file;

            public static void main(String[] args) {

                        try {

                                    fileName = "exercise.java";

                                    file = new File("E:" + "\", fileName);

                                    if (file.exists()) {

//                                              file.delete();

                                    } else {

                                                file.createNewFile();

                                    }

                                   

                                    if (file.isDirectory()) {

                                                System.out.println("this file is directory");

                                    }

                                   

                                    if (file.isFile()) {

                                                System.out.println("this file is a file");

                                    }

                                   

                                   

                                    FileWriter fw = new FileWriter("E:" + "\" + fileName);

                                    // 缓冲写入文件,并且可以通过转义符或是bw.newLine();来换行

                                    BufferedWriter bw = new BufferedWriter(fw);

//                                  将字符串写入文件

                                    bw.write(" 大家好!");

                                    bw.newLine();

                                    bw.write("本?是《JSP?程技巧》");

                                    bw.newLine();

                                    bw.write("?多多指教!");

                                    bw.newLine();

                                    bw.write("email:stride@sina.comqianyf");

                                    bw.flush();

                                    bw.close();

                                    //在文件制定位置追加内容

                                    RandomAccessFile rf = new RandomAccessFile("E:" + "\" + fileName, "rw");

                                    rf.seek(rf.length());

                                    rf.writeBytes(" zui jia  de");

                                    rf.close();

                                   

                                    // 通过缓冲来读文件

                                    FileReader fr = new FileReader("E:" + "\" + fileName);

                                    BufferedReader br = new BufferedReader(fr);

                                    String lineData = br.readLine();

                                    while (null != lineData) {

                                                System.out.println(lineData);

                                                lineData = br.readLine();

                                    }

                                   

                                               

                      } catch (IOException ee) {

                                    System.out.println("System Exception");

                      }

                       

            }

}


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms