導航:首頁 > 文件教程 > jfilechooser保存文件

jfilechooser保存文件

發布時間:2023-07-16 20:38:01

java 中怎麼通過文件選擇器選擇要保存文件的的目錄,然後返回絕對路徑

如果用的是JFileChooser的話可以用getCurrentDirectory().getPath();方法得到你選擇的文件夾路徑。

② Java (文件選擇器) 保存文件

JFileChooser fd=new JFileChooser();
fd.showSaveDialog(null);
File f=fd.getSelectedFile();
System.out.println(f.getName());

疑惑解答:
1. JFileChooser.getSelectedFile()返回一個文件對象,調這個文件對象的getName()很容易得到用專戶輸入的文件名。返迴文件對象既屬包含了文件路徑也包含了文件名,這也體現了Java面向對象的思想。
2. Java的文件對象在文件系統中是可以存在,也可不存在的,所以Java的文件對象有exists()、
createNewFile()、mkdir()等方法。所以文件保存對話框返回的文件對象不一定在文件系統實際存在,而僅僅是一串路徑的表示。

③ java 如何做 保存文件時候彈出的那個窗口有

JFileChooser jf=new JFileChooser("d:/");
int value=jf.showSaveDialog(null);

if(value==JFileChooser.APPROVE_OPTION){ //判斷窗口是否點的是打開或保存

File getPath=jf.getSelectedFile(); //取得路徑
.......//用流在將你的數據輸出

}else{
// 沒有選擇,即點了內窗口的取消
....... //點了取容消後有要做些什麼
}

注意:這個只是路徑,通過這個路徑你可以用流來輸出你的數據。
千萬不要以為你只選擇點保存打開它就幫你自動保存。

④ JFileChooser 保存上次打開文件的路徑

量身打造 請測試...import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Properties;public class FileChooser extends JFrame implements ActionListener{
private JButton open;
private JFileChooser chooser;
private String latestPath ;
private Profile profile;
public FileChooser() {
profile = new Profile();//每次運行程序時創建配置文件Profile對象
latestPath = (profile.read()?profile.latestPath:"C:/");//讀取配置文件里的參數Profile並賦值給latestPath
open = new JButton("open");
this.add(open);
this.setSize(200,150);
this.setLocationRelativeTo(null);//窗口居中
this.setVisible(true);
open.addActionListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
profile.write(latestPath);//每次退出程序時把最後一次打開的目錄寫入到配置文件
}
});
} public static void main(String[] args) {
new FileChooser();
} public void actionPerformed(ActionEvent arg0) {
if(!new File(latestPath).exists()){
latestPath = "C:/";
}else{
chooser = new JFileChooser(latestPath);
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
//其他操作...
latestPath = file.getParent();//每次退出文件選擇器後更新目錄Properties
}
}
}
}class Profile{
String latestPath = "C:/";
File file = new File("C:/JavaCode/FileChooser/set.ini");
public Profile(){}

boolean create(){
boolean b = true;
if(file!=null){
File directory = file.getParentFile();//獲得文件的父目錄
if(!directory.exists()){//目錄不存在時
b = directory.mkdirs();//創建目錄
}else{//存在目錄
if(!file.exists()){//配置文件不存在時
try {
b = file.createNewFile();//創建配置文件
} catch (IOException e) {
b = false;
}
}
}
} return b;
}

boolean read(){
Properties pro;//屬性集
FileInputStream is = null;
boolean b = true;
if(!file.exists()){//配置文件不存在時
b = create();//創建一個
if(b)//創建成功後
b = write(latestPath);//初始化
else//創建失敗即不存在配置文件時彈出對話框提示錯誤
JOptionPane.showConfirmDialog(null, "對不起,不存在配置文件!", "錯誤",
JOptionPane.YES_NO_OPTION,
JOptionPane.ERROR_MESSAGE);
}else{
try {
is = new FileInputStream(file);
pro = new Properties();
pro.load(is);//讀取屬性
latestPath = pro.getProperty("latestPath");//讀取配置參數latestPath的值
is.close();
}
catch (IOException ex) {
ex.printStackTrace();
b = false;
}
}
return b;
}

boolean write(String latestPath){
this.latestPath = latestPath;
Properties pro = null;
FileOutputStream os = null;
boolean b = true;
try {
os = new FileOutputStream(file);
pro = new Properties();

pro.setProperty("latestPath",latestPath);

pro.store(os,null); //將屬性寫入
os.flush();
os.close();

System.out.println("latestPath=" + latestPath);

}
catch (IOException ioe) {
b = false;
ioe.printStackTrace();
}
return b;
}
}

⑤ java中JFileChooser保存文件獲取要把文件保存到的路徑

先保證遠程計算機的目錄可以寫入,然後和操作本地文件沒什麼區別,一個示例:

try{
FileOutputStream fos = new FileOutputStream(new File("\\\\192.168.0.2\\a.txt"));
}
catch(Exception ex){
ex.printStackTrace();
}

這可以在192.168.0.2的共享根目錄下創建一個a.txt文件,詳細的文件操作優化方面,可以自己看看參考資料的。

⑥ java編寫的記事本的保存和另存為功能

你要的關鍵程序代碼:。(源文件超過10000字,無法粘全在這,要的話,加我)(e.getActionCommand()=="保存"||e.getActionCommand()=="保存為")
{
JFileChooser chooser=new JFileChooser(); //創建文件選擇對話框
int result=chooser.showSaveDialog(frame);
if(result==JFileChooser.APPROVE_OPTION)
{

try
{
FileWriter fout = new FileWriter(chooser.getSelectedFile()); //向磁碟中寫文件
fout.write(jtextpane.getText()+"\r\n");
fout.close();
}
catch (IOException ioex)
{

}
}

}執行效果:

閱讀全文

與jfilechooser保存文件相關的資料

熱點內容
word2010頁碼左右設置 瀏覽:907
tpg文件 瀏覽:517
edusoho安裝教程 瀏覽:251
編程貓如何申請退課 瀏覽:794
紅米怎麼用文件管理下載qq視頻 瀏覽:560
自己要解壓的文件找不到怎麼辦 瀏覽:296
文件管理的圖片可以搜回來嗎 瀏覽:369
mini下載的文件在哪裡 瀏覽:973
mfrc522射頻識別參考程序注釋new 瀏覽:363
手機儲存相機照片文件管理找不到 瀏覽:507
天刀詞綴升級被鎖 瀏覽:956
安化哪裡可以學編程 瀏覽:716
純文本文件和二進制文件 瀏覽:216
如何把網路玩起來 瀏覽:654
錄視頻文件特別小的軟體 瀏覽:75
vivo隱藏文件夾在哪裡設置 瀏覽:790
都有哪些圈錢APP 瀏覽:434
javaweb開發pdf 瀏覽:313
ps保存文件特大 瀏覽:46
網路和編程哪個更吃香 瀏覽:583

友情鏈接