Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何创建目录并在计算机上查看它_Java_Android_Directory - Fatal编程技术网

Java 如何创建目录并在计算机上查看它

Java 如何创建目录并在计算机上查看它,java,android,directory,Java,Android,Directory,我尝试在平板电脑上创建目录,并希望看到它 我用这个代码创建目录 public void createDirectory(String sDirectoryName) { File direct = getDir(sDirectoryName, Context.MODE_PRIVATE); File fileWithinMyDir = new File(direct, "myfile"); if(!direct.exist()) { System.out.println("

我尝试在平板电脑上创建目录,并希望看到它

我用这个代码创建目录

public void createDirectory(String sDirectoryName) {
  File direct = getDir(sDirectoryName, Context.MODE_PRIVATE);
  File fileWithinMyDir = new File(direct, "myfile");

  if(!direct.exist()) {
     System.out.println("Directory created");
  }
  else {
     System.out.println("Directory not created");
  }
}
我每次都能看到创建的目录,但当我在文件系统中搜索文件夹时,我看不到它。我如何使它可见。我做错了吗

编辑:

我在舱单上给予了所有的许可。我也试过这个代码

File direct = new File(Environment.getExternalStorageDirectory()+"/"+sDirectoryName);

        if(!direct.exists())
        {
             if(direct.mkdir())
             {
                 System.out.println("Directory created");
                 Toast.makeText(MainActivity.this, "Directory created", Toast.LENGTH_LONG).show();
             }
             else
             {
                 System.out.println("Directory not created");
                 Toast.makeText(MainActivity.this, "Directory not created", Toast.LENGTH_LONG).show();
             }
        }
但这对我也不起作用

编辑: 对于刷新,我使用此代码

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
正在工作。

要创建目录:

if(!direct.exist()) {
   if (direct.mkdir())
      Log.i(TAG, "Directory created");
   else
      Log.w(TAG, "Failed to create directory");
}
不要忘记清单文件中的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

如果文件不存在,则不会创建文件。它只存储指向它的路径。您的if语句显示它不存在。

试试这个

public void createDirectory(String sDirectoryName)
            {    
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), sDirectoryName);
                                if (!file.exists()) {
                                    file.mkdirs();

                            }
    }

使用下面的代码创建目录

public void createDirectory(String sDirectoryName) {
  File direct = getDir(sDirectoryName, Context.MODE_PRIVATE);
  File fileWithinMyDir = new File(direct, "myfile");

  if(!direct.exist()) {
     direct.mkdirs();
     System.out.println("Directory created");
  }
  else {
     System.out.println("Directory not created");
  }
}
在AndroidManifest.xml中添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

确保您的曼尼菲主义者具有以下权限

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在清单中添加以下内容:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
要删除它:

 myDirectory.delete();
对于父目录的文件对象:

 //create a File object for the parent directory

 File wallpaperDirectory = new File("/sdcard/Wallpaper/");
 // have the object build the directory structure, if needed.
 wallpaperDirectory.mkdirs();
 // create a File object for the output file
 File outputFile = new File(wallpaperDirectory, filename);
 // now attach the OutputStream to the file object, instead of a String representation
 FileOutputStream fos = new FileOutputStream(outputFile);

注意:由于Android使用MTP协议进行USB连接,有时文件或文件夹无法显示,因为所有内容都已缓存,可能需要刷新


更多信息:

您的打印声明令人困惑:

if(!direct.exist()) { // If directory does not exist
     System.out.println("Directory created"); // Directory created not true
}
仅创建
文件
对象时,不会创建目录,代码应为:

if(!direct.exist()) { // If directory does not exist
     direct.mkdir(); // Create directory
     System.out.println("Directory created");
}
else {
     System.out.println("Directory not created");
}
还要确保在应用程序中添加android.permission.WRITE_EXTERNAL_STORAGE权限


此外,建议不要像在模拟器上那样在Android中使用,大多数设备
System.out.println
被重定向到LogCat并使用
Log.i()打印。这在非常旧的或定制的Android版本上可能不是真的。

问题不在这条线上吗
File direct=getDir(sddirectoryname,Context.MODE\u PRIVATE)

根据文档context.MODE_PRIVATE仅在应用程序本身中可见,其他程序或用户ID将无法找到它

尝试:

File direct=getDir(sddirectoryname,Context.MODE\u WORLD\u READABLE)

File direct=getDir(sddirectoryname,Context.MODE\u WORLD\u WRITEABLE)


检查
.mkdir()
结果!我也希望看到我计算机上的文件夹。也许它工作正常,但我想看看它保存文件。内部sd卡,然后搜索你的文件夹我没有sd卡。我将其连接到计算机并尝试搜索。基本我想在我的电脑上看到它在你的电脑上连接设备时,在窗口浏览器中创建便携式设备文件夹打开并搜索文件夹
if(!direct.exist()) { // If directory does not exist
     System.out.println("Directory created"); // Directory created not true
}
if(!direct.exist()) { // If directory does not exist
     direct.mkdir(); // Create directory
     System.out.println("Directory created");
}
else {
     System.out.println("Directory not created");
}