本文目錄一覽:
- 1、怎麼打開java文件
- 2、java 如何打開一個文件夾
- 3、java 如何打開文件夾(包括文件夾內的文件夾)!
- 4、怎樣用java中的相對路徑打開文件?
- 5、Java打開指定文件
- 6、【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