मैं अपनी तालिका में अपना संपर्क सहेजने की कोशिश कर रहा हूं लेकिन फाइलचॉसर हमेशा सभी फाइलों पर सेट हो जाता है। वैसे ही मैं इसे केवल .txt स्वीकार करने के लिए सेट कर सकता हूं और इसे डिफ़ॉल्ट या एकमात्र विकल्प बना सकता हूं।मैं jfilechooser को केवल स्वीकार कैसे करूं .txt
savecontact.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser filesave = new JFileChooser();
int returnVal = filesave.showSaveDialog(Main.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
File file = filesave.getSelectedFile();
PrintWriter os = new PrintWriter(file);
os.println("");
for (int col = 0; col < table.getColumnCount(); col++) {
os.print(table.getColumnName(col) + "\t");
}
os.println("");
os.println("");
for (int row = 0; row < table.getRowCount(); row++) {
for (int col = 0; col < table.getColumnCount(); col++) {
os.print(table.getColumnName(col));
os.print(": ");
os.println(table.getValueAt(row, col));
}
os.println("");
}
os.close();
System.out.println("Done!");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
यह हो नहीं करना चाहिए 'setFileFilter (...)'? –
कोड काम करता है लेकिन मुझे अभी भी फ़ाइल के अंत में .txt एक्सटेंशन टाइप करना होगा। क्या कोई तरीका है कि मैं इसे डिफ़ॉल्ट रूप से .txt के रूप में सहेज सकता हूं –
'फ़ाइल फ़ाइल = filesave.getSelectedFile();' कॉल करने के बाद, आप जांच सकते हैं कि फ़ाइल नाम में एक्सटेंशन "txt" है या नहीं। यदि नहीं, तो आप एक्सटेंशन वाली एक नई फ़ाइल ऑब्जेक्ट बनाते हैं और फिर 'नया प्रिंटवाइटर (फ़ाइल 2) के साथ जारी रखें;' (एक नया 'फ़ाइल' ऑब्जेक्ट बनाना वास्तव में फ़ाइल नहीं बनाता है।) – Enwired