真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

另存為java代碼實(shí)現(xiàn) java實(shí)現(xiàn)文件另存為

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

可以通過“FileOutputStream”創(chuàng)建文件實(shí)例,之后過“OutputStreamWriter”流的形式進(jìn)行存儲(chǔ),舉例:

成都創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),紫云網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:紫云等地區(qū)。紫云做網(wǎng)站價(jià)格咨詢:18980820575

OutputStreamWriter

pw

=

null;//定義一個(gè)流

pw

=

new

OutputStreamWriter(new

FileOutputStream(“D:/test.txt”),"GBK");//確認(rèn)流的輸出文件和編碼格式,此過程創(chuàng)建了“test.txt”實(shí)例

pw.write("我是要寫入到記事本文件的內(nèi)容");//將要寫入文件的內(nèi)容,可以多次write

pw.close();//關(guān)閉流

解釋:文件流用完之后必須及時(shí)通過close方法關(guān)閉,否則會(huì)一直處于打開狀態(tài),直至程序停止,增加系統(tǒng)負(fù)擔(dān)。

如何用Java實(shí)現(xiàn)另存為

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è)置打開對(duì)話框的默認(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();

}

}

用java文件對(duì)話框?qū)崿F(xiàn)文件另存為

我覺得用一個(gè)文件復(fù)制的類就可以實(shí)現(xiàn)備份,我大概寫了一個(gè),基本功能可以實(shí)現(xiàn),但是總覺得好怪

你可以給JTextField t1 一個(gè)初始路徑,那樣,如果每次都是備份同一個(gè)文件直接點(diǎn)保存選路徑就可以,如果想備份其他文件再選其他文件就可以了

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è)置打開對(duì)話框的默認(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();

}

}

java圖形用戶界面的選擇一個(gè)文件并復(fù)制(另存為)的代碼,麻煩了。

import?java.awt.EventQueue;

import?javax.swing.JFrame;

import?javax.swing.JLabel;

import?javax.swing.JOptionPane;

import?java.awt.Font;

import?javax.swing.JTextField;

import?javax.swing.JButton;

import?java.awt.Color;

import?java.awt.event.ActionListener;

import?java.awt.event.ActionEvent;

import?java.io.File;

import?java.io.FileInputStream;

import?java.io.FileOutputStream;

import?java.io.InputStream;

import?javax.swing.JFileChooser;

public?class?CopyFile?{

private?JFrame?frame;

private?JTextField?textField;

private?JTextField?textField_1;

private?JFileChooser?chooser;

private?String?readPath;

private?String?writePath;

/**

?*?Launch?the?application.

?*/

public?static?void?main(String[]?args)?{

EventQueue.invokeLater(new?Runnable()?{

public?void?run()?{

try?{

CopyFile?window?=?new?CopyFile();

window.frame.setVisible(true);

}?catch?(Exception?e)?{

e.printStackTrace();

}

}

});

}

/**

?*?Create?the?application.

?*/

public?CopyFile()?{

initialize();

}

/**

?*?Initialize?the?contents?of?the?frame.

?*/

private?void?initialize()?{

frame?=?new?JFrame();

frame.setBounds(100,?100,?545,?277);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

JLabel?label?=?new?JLabel("\u6587\u4EF6\uFF1A");

label.setFont(new?Font("黑體",?Font.BOLD,?18));

label.setBounds(26,?68,?57,?25);

frame.getContentPane().add(label);

JLabel?lblNewLabel?=?new?JLabel("\u4FDD\u5B58\u76EE\u5F55\uFF1A");

lblNewLabel.setFont(new?Font("黑體",?Font.BOLD,?18));

lblNewLabel.setBounds(10,?119,?95,?25);

frame.getContentPane().add(lblNewLabel);

textField?=?new?JTextField();

textField.setBounds(105,?68,?299,?25);

frame.getContentPane().add(textField);

textField.setColumns(10);

textField_1?=?new?JTextField();

textField_1.setBounds(105,?121,?299,?25);

frame.getContentPane().add(textField_1);

textField_1.setColumns(10);

chooser?=?new?JFileChooser();

chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//?設(shè)置選擇模式,既可以選擇文件又可以選擇文件夾

JButton?button?=?new?JButton("\u6253\u5F00");

button.addActionListener(new?ActionListener()?{

public?void?actionPerformed(ActionEvent?e)?{

int?index?=?chooser.showOpenDialog(null);

chooser.setDialogType(JFileChooser.OPEN_DIALOG);

chooser.setMultiSelectionEnabled(false);

chooser.setAcceptAllFileFilterUsed(false);

if?(index?==?JFileChooser.APPROVE_OPTION)?{

//?把獲取到的文件的絕對(duì)路徑顯示在文本編輯框中

textField.setText(chooser.getSelectedFile()

.getAbsolutePath());

readPath?=?textField.getText();

}

}

});

button.setFont(new?Font("黑體",?Font.BOLD,?18));

button.setBounds(432,?67,?87,?26);

frame.getContentPane().add(button);

JButton?button_1?=?new?JButton("\u6D4F\u89C8");

button_1.addActionListener(new?ActionListener()?{

public?void?actionPerformed(ActionEvent?e)?{

int?index?=?chooser.showSaveDialog(null);

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

chooser.setDialogType(JFileChooser.SAVE_DIALOG);

chooser.setMultiSelectionEnabled(false);

chooser.setAcceptAllFileFilterUsed(false);

if?(index?==?JFileChooser.APPROVE_OPTION)?{

//?把獲取到的文件的絕對(duì)路徑顯示在文本編輯框中

textField_1.setText(chooser.getSelectedFile()

.getAbsolutePath());

writePath?=?textField_1.getText()?+?"\\";

}

}

});

button_1.setFont(new?Font("黑體",?Font.BOLD,?18));

button_1.setBounds(432,?118,?87,?26);

frame.getContentPane().add(button_1);

JButton?button_2?=?new?JButton("\u53E6\u5B58\u4E3A");

button_2.addActionListener(new?ActionListener()?{

public?void?actionPerformed(ActionEvent?e)?{

readPath?=?textField.getText();

writePath?=?textField_1.getText()?+?"\\";

if(copyFile(readPath,?writePath)==?-1){//原文件不存在

JOptionPane.showMessageDialog(null,?"源文件不存在",?"警告",?JOptionPane.ERROR_MESSAGE);

}

}

});

button_2.setForeground(Color.RED);

button_2.setFont(new?Font("黑體",?Font.BOLD,?18));

button_2.setBounds(213,?180,?93,?34);

frame.getContentPane().add(button_2);

}

/*

?*?*

?*?復(fù)制單個(gè)文件

?*?

?*?@param?oldPath?String?原文件路徑?如:c:/fqf.txt

?*?

?*?@param?newPath?String?復(fù)制后路徑?如:f:/fgf.txt

?*?

?*?@return?int?0表示成功,-1表示原文件不存在,-2表示未知錯(cuò)誤。

?*/

public?int?copyFile(String?oldPath,?String?newPath)?{

try?{

int?bytesum?=?0;

int?byteread?=?0;

File?oldfile?=?new?File(oldPath);

if?(oldfile.exists())?{?//?文件存在時(shí)

InputStream?inStream?=?new?FileInputStream(oldPath);?//?讀入原文件

System.out.println(newPath);

if(isExist(newPath)){

FileOutputStream?fs?=?new?FileOutputStream(newPath);

byte[]?buffer?=?new?byte[1444];

while?((byteread?=?inStream.read(buffer))?!=?-1)?{

bytesum?+=?byteread;?//?字節(jié)數(shù)?文件大小

System.out.println(bytesum);

fs.write(buffer,?0,?byteread);

}

inStream.close();

fs.close();

return?0;

}else{

return?-2;

}

}

return?-1;

}?catch?(Exception?e)?{

System.out.println("復(fù)制單個(gè)文件操作出錯(cuò)");

e.printStackTrace();

return?-2;

}

}

public?static?boolean?isExist(String?filePath)?{

String?paths[]?=?filePath.split("\\\\");

String?dir?=?paths[0];

for?(int?i?=?0;?i??paths.length?-?2;?i++)?{//?注意此處循環(huán)的長度

try?{

dir?=?dir?+?"/"?+?paths[i?+?1];

File?dirFile?=?new?File(dir);

if?(!dirFile.exists())?{

dirFile.mkdir();

System.out.println("創(chuàng)建目錄為:"?+?dir);

}

}?catch?(Exception?err)?{

System.err.println("ELS?-?Chart?:?文件夾創(chuàng)建發(fā)生異常");

}

}

File?fp?=?new?File(filePath);

if?(!fp.exists())?{

return?true;?//?文件不存在,執(zhí)行下載功能

}?else?{

return?false;?//?文件存在不做處理

}

}

}


當(dāng)前標(biāo)題:另存為java代碼實(shí)現(xiàn) java實(shí)現(xiàn)文件另存為
網(wǎng)站鏈接:http://weahome.cn/article/hhoigj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部