java打開文件夾,java打開文件夾取消空指針

本文目錄一覽:

怎麼打開java文件

在啟動Eclipse後左邊方框中單機右鍵,在右鍵菜單中選擇導入(英文版Eclipse選import)選項,然後選擇已存在的項目(英文版Existing Project into Workplace),在所有java文件前選對號後之後點完成(英文版finish),就完成了java程序的導入,在打開要運行的代碼頁面,然後在上面的工具欄中點運行那個綠色按鈕就可以運行了。

java 如何打開一個文件夾

給你一段文件操作的例子

package com.file.sample;

import java.io.*;

public class FileOperate {

public FileOperate() {

}

/**

* 新建目錄

*

* @param folderPath

* String 如 c:/fqf

* @return boolean

*/

public void newFolder(String folderPath) {

try {

String filePath = folderPath;

filePath = filePath.toString();

java.io.File myFilePath = new java.io.File(filePath);

if (!myFilePath.exists()) {

myFilePath.mkdir();

}

} catch (Exception e) {

System.out.println(“新建目錄操作出錯”);

e.printStackTrace();

}

}

/**

* 新建文件

*

* @param filePathAndName

* String 文件路徑及名稱 如c:/fqf.txt

* @param fileContent

* String 文件內容

* @return boolean

*/

public void newFile(String filePathAndName, String fileContent) {

try {

String filePath = filePathAndName;

filePath = filePath.toString();

File myFilePath = new File(filePath);

if (!myFilePath.exists()) {

myFilePath.createNewFile();

}

FileWriter resultFile = new FileWriter(myFilePath);

PrintWriter myFile = new PrintWriter(resultFile);

String strContent = fileContent;

myFile.println(strContent);

resultFile.close();

} catch (Exception e) {

System.out.println(“新建目錄操作出錯”);

e.printStackTrace();

}

}

/**

* 刪除文件

*

* @param filePathAndName

* String 文件路徑及名稱 如c:/fqf.txt

* @param fileContent

* String

* @return boolean

*/

public void delFile(String filePathAndName) {

try {

String filePath = filePathAndName;

filePath = filePath.toString();

java.io.File myDelFile = new java.io.File(filePath);

myDelFile.delete();

} catch (Exception e) {

System.out.println(“刪除文件操作出錯”);

e.printStackTrace();

}

}

/**

* 刪除文件夾

*

* @param filePathAndName

* String 文件夾路徑及名稱 如c:/fqf

* @param fileContent

* String

* @return boolean

*/

public void delFolder(String folderPath) {

try {

delAllFile(folderPath); // 刪除完裏面所有內容

String filePath = folderPath;

filePath = filePath.toString();

java.io.File myFilePath = new java.io.File(filePath);

myFilePath.delete(); // 刪除空文件夾

} catch (Exception e) {

System.out.println(“刪除文件夾操作出錯”);

e.printStackTrace();

}

}

/**

* 刪除文件夾裏面的所有文件

*

* @param path

* String 文件夾路徑 如 c:/fqf

*/

public void delAllFile(String path) {

File file = new File(path);

if (!file.exists()) {

return;

}

if (!file.isDirectory()) {

return;

}

String[] tempList = file.list();

File temp = null;

for (int i = 0; i tempList.length; i++) {

if (path.endsWith(File.separator)) {

temp = new File(path + tempList[i]);

} else {

temp = new File(path + File.separator + tempList[i]);

}

if (temp.isFile()) {

temp.delete();

}

if (temp.isDirectory()) {

delAllFile(path + “/” + tempList[i]);// 先刪除文件夾裏面的文件

delFolder(path + “/” + tempList[i]);// 再刪除空文件夾

}

}

}

/**

* 複製單個文件

*

* @param oldPath

* String 原文件路徑 如:c:/fqf.txt

* @param newPath

* String 複製後路徑 如:f:/fqf.txt

* @return boolean

*/

public void copyFile(String oldPath, String newPath) {

try {

int bytesum = 0;

int byteread = 0;

File oldfile = new File(oldPath);

if (oldfile.exists()) { // 文件存在時

InputStream inStream = new FileInputStream(oldPath); // 讀入原文件

FileOutputStream fs = new FileOutputStream(newPath);

byte[] buffer = new byte[1444];

int length;

while ((byteread = inStream.read(buffer)) != -1) {

bytesum += byteread; // 位元組數 文件大小

System.out.println(bytesum);

fs.write(buffer, 0, byteread);

}

inStream.close();

}

} catch (Exception e) {

System.out.println(“複製單個文件操作出錯”);

e.printStackTrace();

}

}

/**

* 複製整個文件夾內容

*

* @param oldPath

* String 原文件路徑 如:c:/fqf

* @param newPath

* String 複製後路徑 如:f:/fqf/ff

* @return boolean

*/

public void copyFolder(String oldPath, String newPath) {

try {

(new File(newPath)).mkdirs(); // 如果文件夾不存在 則建立新文件夾

File a = new File(oldPath);

String[] file = a.list();

File temp = null;

for (int i = 0; i file.length; i++) {

if (oldPath.endsWith(File.separator)) {

temp = new File(oldPath + file[i]);

} else {

temp = new File(oldPath + File.separator + file[i]);

}

if (temp.isFile()) {

FileInputStream input = new FileInputStream(temp);

FileOutputStream output = new FileOutputStream(newPath

+ “/” + (temp.getName()).toString());

byte[] b = new byte[1024 * 5];

int len;

while ((len = input.read(b)) != -1) {

output.write(b, 0, len);

}

output.flush();

output.close();

input.close();

}

if (temp.isDirectory()) {// 如果是子文件夾

copyFolder(oldPath + “/” + file[i], newPath + “/” + file[i]);

}

}

} catch (Exception e) {

System.out.println(“複製整個文件夾內容操作出錯”);

e.printStackTrace();

}

}

/**

* 移動文件到指定目錄

*

* @param oldPath

* String 如:c:/fqf.txt

* @param newPath

* String 如:d:/fqf.txt

*/

public void moveFile(String oldPath, String newPath) {

copyFile(oldPath, newPath);

delFile(oldPath);

}

/**

* 移動文件到指定目錄

*

* @param oldPath

* String 如:c:/fqf.txt

* @param newPath

* String 如:d:/fqf.txt

*/

public void moveFolder(String oldPath, String newPath) {

copyFolder(oldPath, newPath);

delFolder(oldPath);

}

public static void main(String[] args){

FileOperate filedemo=new FileOperate();

filedemo.delAllFile(“d:/test”);

}

}

java 如何打開文件夾(包括文件夾內的文件夾)!

文件太多,過濾了下後綴為.mp3的所有文件:

package cn.zhidao.file;

import java.io.File;

/**

 * 

 * @author Administrator

 * 

 */

public class Read {

    public static void main(String[] args) {

        getFileName(“D:”);

    }

    public static void getFileName(String path) {

        File file = new File(path);

        if (file.isDirectory()) {

            File[] dirFile = file.listFiles();

            if (dirFile!=null) {

                for (File f : dirFile) {

                    if (f.isDirectory())

                        getFileName(f.getAbsolutePath());

                    else {

                        if (f.getAbsolutePath().endsWith(“.mp3”)) {

                            System.out.println(f.getAbsolutePath());

                        }

                    }

                }

            }

        }

    }

}

列出所有文件將 if (f.getAbsolutePath().endsWith(“.mp3”))

刪除!

結果:

D:\KwDownloadx\Song\劉珂矣-半壺紗.mp3

D:\KwDownloadx\Song\劉珂矣-芙蓉雨.mp3

D:\KwDownloadx\Song\墨明棋妙-蘭若詞 (墨明棋妙版).mp3

D:\KwDownloadx\Song\孫子涵-唐人.mp3

D:\KwDownloadx\Song\孫露-不甘寂寞 – 中四 2012新概念.mp3

D:\KwDownloadx\Song\孫露-你是我今生最愛的女孩.mp3

D:\KwDownloadx\Song\孫露-我的愛情被你燒成灰.mp3

D:\KwDownloadx\Song\孫露-離別的秋天.mp3

D:\KwDownloadx\Song\小5-離殤.mp3

D:\KwDownloadx\Song\崔子格徐譽滕-相思網.mp3

D:\KwDownloadx\Song\影視原聲-殺破狼.mp3

D:\KwDownloadx\Song\心然-千年緣(《仙劍奇俠傳四》遊戲插曲).mp3

D:\KwDownloadx\Song\董貞-白素貞.mp3

D:\KwDownloadx\Song\藍心湄-一見鍾情.mp3

D:\KwDownloadx\Temp\05E1D065515DA6EE.mp3

D:\KwDownloadx\Temp\2F88AECE3F82FCF8.mp3

D:\KwDownloadx\Temp\5698305283F21441.mp3

D:\KwDownloadx\Temp\6087F8E0D5464C96.mp3

D:\KwDownloadx\Temp\A70A6A5436669AB1.mp3

D:\KwDownloadx\Temp\BADF8AD408BE33A5.mp3

D:\KwDownloadx\Temp\C0F80A936A01A2E1.mp3

D:\KwDownloadx\Temp\D33494CB9EB6FFA4.mp3

D:\KwDownloadx\Temp\E9261B021C2BF6C2.mp3

D:\KwDownloadx\Temp\EBDB7CF54D4D2922.mp3

D:\SoftWare\編程軟件\android-sdk_r21.1-windows\android-sdk-windows\android-4\samples\ApiDemos\res\raw\test_cbr.mp3

D:\SoftWare\編程軟件\android-sdk_r21.1-windows\android-sdk-windows\platforms\android-4\samples\ApiDemos\res\raw\test_cbr.mp3

D:\SoftWare\編程軟件\android-sdk_r21.1-windows\android-sdk-windows\samples\android-17\ApiDemos\res\raw\test_cbr.mp3

怎樣用java中的相對路徑打開文件?

如果你的生成File的文件和a.jpg在同一級的話。你用File f=new File(“a.jpg”);\x0d\x0a如果你的生成File的文件和a.jpg不在同一級的話:1.File在外,a.jpg在pic文件夾則File f=new File(“pic/a.jpg”);\x0d\x0a2.File在file文件夾,a.jpg在pic文件夾則File f=new File(“../pic/a.jpg”);\x0d\x0a3.File在file文件夾,a.jpg外則File f=new File(“../a.jpg”);

Java打開指定文件

java打開文件夾使用方法:

String strTmp= “D:\abc\”;

Runtime.getRuntime().exec(“explorer.exe” + strTmp);

java讀取文件使用方法:

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public class OpenFile {

public static void main(String args[]) {

try {

BufferedReader br = new BufferedReader(new FileReader(“c://EmailSpider.java”));

String line = “”;

while((line=br.readLine()) != null) {

System.out.println(line);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

【java】如何實現「打開文件夾」對話框

JFileChooser類吧,在按鈕監聽器ActionListener的actionPerformed()方法里添加以下代碼就可以了,這樣一點擊這個按鈕,就會彈出來:

JFileChooser chooser = new JFileChooser();

// DIRECTORIES_ONLY就是只選目錄

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

int r = chooser.showOpenDialog(null);

原創文章,作者:EMQBH,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/329942.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
EMQBH的頭像EMQBH
上一篇 2025-01-14 18:55
下一篇 2025-01-14 18:55

相關推薦

  • Idea新建文件夾沒有java class的解決方法

    如果你在Idea中新建了一個文件夾,卻沒有Java Class,應該如何解決呢?下面從多個方面來進行解答。 一、檢查Idea設置 首先,我們應該檢查Idea的設置是否正確。打開Id…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發佈。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29

發表回復

登錄後才能評論