- File directory = new File("");//设定为当前文件夹
- try{
- System.out.println(directory.getCanonicalPath());//获取标准的路径
- System.out.println(directory.getAbsolutePath());//获取绝对路径
- }catch(Exception e){}
- private static File getFileAbsolutePath(String filename) {
- String basePath = new File("").getAbsolutePath();
- String filePath = "src/test/java";
- String packagePath = "com/alibaba/panama/qdox";
- return new File(basePath, File.separator + filePath + File.separator + packagePath
- + File.separator + filename);
- }
String filename="d:\\upload\\133.jpg"; String name[] = filename.split("\\\\");/ 应该这样才对,本身\只是一个转义符,当要用做字符是需要\\,所以如果只有一个\就出错了哦
- /**
- * 取得某目录下面的文件,增加过滤功能,例如:去掉.svn隐藏文件
- *
- * @param dir
- * @return
- */
- public static List<File> getSubFiles(File dir, String filter) {
- if (dir == null || "".equals(dir))
- return null;
- CopyOnWriteArrayList<File> subFiles = new CopyOnWriteArrayList<File>();
- for (File file : getAllSubFiles(dir)) {
- subFiles.add(file);
- }
- for (File file : subFiles) {
- String tmp = file.toString();
- String[] subBlocks = tmp.split("\\\\");
- for (String subBlock : subBlocks) {
- if (subBlock.startsWith(filter)) {
- subFiles.remove(file);
- break;
- }
- }
- }
- return subFiles;
- }
- /**
- * 取得目录下面的所有文件,包含隐藏文件
- *
- * @param dir
- * @return
- */
- public static List<File> getAllSubFiles(File dir) {
- if (dir == null || "".equals(dir))
- return null;
- if (dir.isDirectory()) {
- File[] subFiles = dir.listFiles();
- for (File file : subFiles) {
- if (file.isDirectory()) {
- getAllSubFiles(new File(dir + File.separator + file.getName()));
- } else {
- allSubFiles.add(file);
- }
- }
- } else {
- allSubFiles.add(dir);
- }
- return allSubFiles;
- }
- File file = new File(System.getProperty("user.dir"));
- FilenameFilter filter = new FilenameFilter() {
- @Override
- public boolean accept(File dir, String name) {
- return !name.startsWith(".");
- }
- };
- for (String filename : file.list(filter)) {
- System.out.println(filename);
- }
节点流,用于从文件中读取或往文件中写入字节流。如果在构造FileOutputStream时,文件已经存在,则覆盖这个文件。针对字节(byte)进行操作,常用的方式为:
读入:【注意】将new String(b,off,len)将字节数组转换为字符串
- FileInputStream fis = new FileInputStream("1.txt");
- byte[] b = new byte[100];
- int len = fis.read(b);
- System.out.println(new String(b,0, len));
- fis.close();
- FileOutputStream fos = new FileOutputStream("1.txt");
- fos.write("hello alibaba".getBytes());
- fos.close();
过滤流,需要使用已经存在的节点流来构造,提供带缓冲的读写,提高了读写的效率。没有增加额外的读写功能。
过滤流,需要使用已经存在的节点流来构造,提供了读写Java中的基本数据类型的功能。增强了八种基本类型的读写,读取的时候需要知道写入时的顺序,否则读取的内容出错。
- import java.io.*;
- class PipedStreamTest
- {
- public static void main(String[] args)
- {
- PipedOutputStream pos=new PipedOutputStream();
- PipedInputStream pis=new PipedInputStream();
- try
- {
- pos.connect(pis);
- new Producer(pos).start();
- new Consumer(pis).start();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- class Producer extends Thread
- {
- private PipedOutputStream pos;
- public Producer(PipedOutputStream pos)
- {
- this.pos=pos;
- }
- public void run()
- {
- try
- {
- pos.write("Hello,welcome you!".getBytes());
- pos.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- class Consumer extends Thread
- {
- private PipedInputStream pis;
- public Consumer(PipedInputStream pis)
- {
- this.pis=pis;
- }
- public void run()
- {
- try
- {
- byte[] buf=new byte[100];
- int len=pis.read(buf);
- System.out.println(new String(buf,0,len));
- pis.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- FileOutputStream fos = new FileOutputStream("1.txt");
- OutputStreamWriter osw = new OutputStreamWriter(fos);
- BufferedWriter bw = new BufferedWriter(osw);
- bw.write("hi, alibaba");
- bw.close();
- FileInputStream fis = new FileInputStream("1.txt");
- InputStreamReader isr = new InputStreamReader(fis);
- BufferedReader br = new BufferedReader(isr);
- String line = br.readLine();
- System.out.println(line);
- String filename = Sample01.class.getResource("/demo.txt").getFile();
- FileOutputStream outputStream = new FileOutputStream(filename);
- FileChannel channel = outputStream.getChannel();
- FileLock fileLock = channel.tryLock();
- if (fileLock != null) {
- Thread.sleep(20000);
- System.out.println("file lock release");
- }
- if (fileLock != null) {
- fileLock.release();
- }
- channel.close();
- outputStream.close();