導航:首頁 > 文件教程 > 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保存文件相關的資料

熱點內容
數控機床常用的編程方法有哪些 瀏覽:467
鐵路與大數據分析產生什麼結果 瀏覽:572
如何把文件轉為種子 瀏覽:59
玩股票杠桿用什麼app 瀏覽:999
怎麼用q幣充qq紅包 瀏覽:140
海外代購app哪個比較好 瀏覽:729
手機改qq密碼怎麼改 瀏覽:238
api壓縮文件夾 瀏覽:847
網路營銷中營銷策略都有哪些 瀏覽:926
mat格式文件數據類型 瀏覽:132
手機文件刪除如何恢復 瀏覽:682
如何計算帶有指數的數據 瀏覽:243
手機數據存儲在主板的哪裡 瀏覽:151
什麼網站物品最實惠 瀏覽:361
win7自帶截圖工具不能用 瀏覽:939
javamail發送html郵件 瀏覽:682
穿越火線槍戰王者游戲錄制在哪個文件夾 瀏覽:10
cad圖復制到另一個cad文件標注變形 瀏覽:198
騰訊大數據平台部屬於哪個群 瀏覽:350
電極自動編程哪個軟體好用 瀏覽:550

友情鏈接