繁体   English   中英

Java中的文件处理,权限被拒绝,路径错误

[英]File handling in java with permission denied and wrong path

我有一个Java类,它读取并为我提供可用于特定给定路径的文件列表。

如果给出正确的路径,这很好,但是如果文件路径不可访问或路径错误,则不会出现异常或错误,

如何处理以下问题,

  1. 给定的路径没有访问权限
  2. 给出了错误的路径。

谢谢

这是我尝试的代码,而jdk version is 1.6

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExceptionInFileHandling {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void GetDirectory(String a_Path, List a_files, List a_folders) throws IOException {
        try {
            File l_Directory = new File(a_Path);
            File[] l_files = l_Directory.listFiles();

            for (int c = 0; c < l_files.length; c++) {
                if (l_files[c].isDirectory()) {
                    a_folders.add(l_files[c].getName());
                } else {
                        a_files.add(l_files[c].getName());
                }
            }
        } catch (Exception ex){
            ex.printStackTrace();
        }

    }
    @SuppressWarnings("rawtypes")
    public static void main(String args[]) throws IOException {

        String filesLocation = "asdfasdf/sdfsdf/";
        List l_Files = new ArrayList(), l_Folders = new ArrayList();
        GetDirectory(filesLocation, l_Files, l_Folders);

        System.out.println("Files");
        System.out.println("---------------------------");
        for (Object file : l_Files) {
            System.out.println(file);
        }
        System.out.println("Done");

    }
}

您将必须使用File类的此类方法。 Exists将告诉您给定的路径是否有效, canRead将确定您是否已读取特定文件/目录的canRead 这里有一些文档

@SuppressWarnings({ "rawtypes", "unchecked" })
    public static void GetDirectory(String a_Path, List a_files, List a_folders) throws IOException {
        try {
            File l_Directory = new File(a_Path);
            **if(l_Directory.exists()){**
                File[] l_files = l_Directory.listFiles();

                for (int c = 0; c < l_files.length; c++) {
                    if (l_files[c].isDirectory()) {
                        a_folders.add(l_files[c].getName());
                    } else {
                            a_files.add(l_files[c].getName());
                    }
                }
            }
        } catch (Exception ex){
            ex.printStackTrace();
        }

    }

解决方案1:要基于操作系统替换文件分隔符,请提供类似线程的答案

Java正则表达式替换基于OS的文件路径

Java中与平台无关的路径

解决方案2:要检查错误的路径,可以使用以下代码。

  @SuppressWarnings({ "rawtypes", "unchecked" })
        public static void GetDirectory(String a_Path, List a_files, List a_folders) throws IOException {
            try {
                if (!new File(a_Path).exists())
                {
                   throw new FileNotFoundException("Given path '"+a_Path+"' is not available");
                }
                File l_Directory = new File(a_Path);
                File[] l_files = l_Directory.listFiles();

                for (int c = 0; c < l_files.length; c++) {
                    if (l_files[c].isDirectory()) {
                        a_folders.add(l_files[c].getName());
                    } else {
                            a_files.add(l_files[c].getName());
                    }
                }
            } 
            catch (FileNotFoundException ex){
               ex.printStackTrace();
            }
            catch (Exception ex){
                ex.printStackTrace();
            }            
        }

我希望这能帮到您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM