重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
//====
成都創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、昭蘇網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、購物商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為昭蘇等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
//MyFrame.java
//====
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
public class MyFrame extends JFrame implements ActionListener {
JMenuBar jmb = new JMenuBar();
JMenu jm = new JMenu("文件");
JMenuItem jmi = new JMenuItem("另存為");
JTextArea jta = new JTextArea(20,60);
public MyFrame(){
jm.add(jmi);
jmb.add(jm);
setJMenuBar(jmb);
jmi.addActionListener(this);
this.add(jta);
}
/**
* @param args
*/
public static void main(String[] args) {
JFrame jf = new MyFrame();
jf.setSize(400,300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//以下是根據(jù)你的方法修改得來
private void xiewenjian(File file) throws Exception
{
FileWriter fr = new FileWriter(file);
BufferedWriter br = new BufferedWriter(fr);
String text = jta.getText();
br.write(text);
br.flush();//刷新輸出流
br.close();//關(guān)閉輸出流
fr.close();//關(guān)閉輸出流
}
public void actionPerformed(ActionEvent arg0) {
JFileChooser choosers = new JFileChooser();
// FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件","txt");
// choosers.setFileFilter(filter);
//用戶選擇的文件路徑
String filePath = "";
//彈出的選擇路徑對話框:只能選擇文件夾
choosers.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//用戶點擊的按鈕:確定/取消
int returnVal = choosers.showSaveDialog(this);
//如果點擊了"確定",取得用戶選擇的路徑.
if(returnVal == JFileChooser.APPROVE_OPTION)
filePath = choosers.getSelectedFile().getAbsolutePath();
//如果點擊了"取消"或關(guān)閉,則不保存
else
return;
//構(gòu)建文件路徑
filePath = filePath + File.separator + "我的記事本.txt";
File file = new File(filePath);
try{
xiewenjian(file);
}
catch(Exception e2)
{
}
}
}
保存代碼功能,可以參考下面的代碼。
代碼如下:
function runCode(obj) {
var winname = window.open('', "_blank", '');
winname.document.open('text/html', 'replace');
winname.opener = null // 防止代碼對父頁面修改
winname.document.write(obj.value);
winname.document.close();
}
function saveCode(obj) {
var winname = window.open('', '_blank', 'top=10000');
winname.document.open('text/html', 'replace');
winname.document.write(obj.value);
winname.document.execCommand('saveas','','');
winname.close();
}
function oCopy(obj){
obj.select();
js=obj.createTextRange();
js.execCommand("Copy");
alert("提示:代碼已經(jīng)被成功復(fù)制!");
}
一般操作Excel有專業(yè)的工具庫來實現(xiàn),操作時,會考慮同時兼容不同版本的excel,你這里將xls轉(zhuǎn)為xlsx,就是版本之間轉(zhuǎn)換,可以參考以下代碼的轉(zhuǎn)換方法,方法還是比較簡單,直接將文件另存為就可以了:
import com.spire.xls.*;
public class ExcelConversion {
public static void main(String[] args) {
Workbook wb = new Workbook();
wb.loadFromFile("test.xls");
wb.saveToFile("toXLSX.xlsx");
}
}
這里代碼編譯環(huán)境為IntelliJ IDEA,jdk是1.8.0版本,使用Excel庫free spire.xls.jar 3.9.1。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class BakTo extends JFrame implements ActionListener {
JLabel l1 = new JLabel("原始文件");
JTextField t1 = new JTextField(40);
JButton b1 = new JButton("選擇");
JLabel l2 = new JLabel("保存目錄");
JTextField t2 = new JTextField(40);
JButton b2 = new JButton("保存");
JFileChooser j1 = new JFileChooser();
JFileChooser j2 = new JFileChooser();
static File fileFlag = new File("");
public BakTo() {
setBounds(200, 200, 600, 140);
setLayout(new FlowLayout());
add(l1);
add(t1);
add(b1);
add(l2);
add(t2);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setResizable(false);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == b1) {
int n = j1.showOpenDialog(null);
String filename = j1.getSelectedFile().toString();
if (n == JFileChooser.APPROVE_OPTION) {
t1.setText(filename);
fileFlag = new File(filename);
}
}
else if (e.getSource() == b2) {
j2.setCurrentDirectory(fileFlag);// 設(shè)置打開對話框的默認(rèn)路徑
j2.setSelectedFile(fileFlag);// 設(shè)置選中原來的文件
int n = j2.showSaveDialog(null);
String filename2 = j2.getSelectedFile().toString();
if(filename2.indexOf(".")!=-1){
filename2=filename2.substring(0,filename2.indexOf("."));
}
// 以下兩句是獲得原文件的擴(kuò)展名
int flag = t1.getText().lastIndexOf(".");
String kuozhan = t1.getText().substring(flag);
String date = getDate();// 取得當(dāng)前日期
if (n == JFileChooser.APPROVE_OPTION) {
t2.setText(filename2 +date+ kuozhan);// 把日期和擴(kuò)展名添加到原來文件的后面
}
int b;
char[] t = new char[25];
// 這里我改用了文件流
FileInputStream input = new FileInputStream(t1.getText());
FileOutputStream output = new FileOutputStream(filename2+date
+ kuozhan);// 把擴(kuò)展名添加到原來文件的后面
int in = input.read();
while (in != -1) {
output.write(in);
in = input.read();
}
input.close();
output.close();
}
} catch (Exception x) {
System.out.println(x);
}
}
public String getDate() {
Calendar rightNow = Calendar.getInstance();
System.out.println(rightNow.toString());
int year = rightNow.YEAR;
int date = rightNow.DATE;
int month = rightNow.MONTH + 1;
String d = year + "年" + month + "月" + date + "日";
return d;
}
public static void main(String args[]) {
BakTo c1 = new BakTo();
}
}