JAVA记事本中另存文件方法

Python014

JAVA记事本中另存文件方法,第1张

//====

//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)

}

//以下是根据你的方法修改得来

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()//关闭输出流

fr.close()//关闭输出流

}

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()

//如果点击了"取消"或关闭,则不保存

else

return

//构建文件路径

filePath = filePath + File.separator + "我的记事本.txt"

File file = new File(filePath)

try{

xiewenjian(file)

}

catch(Exception e2)

{

}

}

}

一般操作Excel有专业的工具库来实现,操作时,会考虑同时兼容不同版本的excel,你这里将xls转为xlsx,就是版本之间转换,可以参考以下代码的转换方法,方法还是比较简单,直接将文件另存为就可以了:

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")

}

}

这里代码编译环境为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)// 设置打开对话框的默认路径

j2.setSelectedFile(fileFlag)// 设置选中原来的文件

int n = j2.showSaveDialog(null)

String filename2 = j2.getSelectedFile().toString()

if(filename2.indexOf(".")!=-1){

filename2=filename2.substring(0,filename2.indexOf("."))

}

// 以下两句是获得原文件的扩展名

int flag = t1.getText().lastIndexOf(".")

String kuozhan = t1.getText().substring(flag)

String date = getDate()// 取得当前日期

if (n == JFileChooser.APPROVE_OPTION) {

t2.setText(filename2 +date+ kuozhan)// 把日期和扩展名添加到原来文件的后面

}

int b

char[] t = new char[25]

// 这里我改用了文件流

FileInputStream input = new FileInputStream(t1.getText())

FileOutputStream output = new FileOutputStream(filename2+date

+ kuozhan)// 把扩展名添加到原来文件的后面

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()

}

}