大体代码:
public class TestOut {
public void TK3(String fileName,String outfileName) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(new File(
fileName)));//从界面获取的输入文件位置
BufferedWriter bw = new BufferedWriter(new FileWriter(outfileName)); //将缓冲对文件的输出,输出
try
{ ...
bw.write(XXXXX);//从inputfileName读取数据输出到outputfileName
bw.newLine();//写一行结束换行
continue;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
bw.flush(); //刷新该流的缓冲
bw.close(); //关闭流,释放资源
reader.close(); //关闭流,释放资源
}
}
public static void main(String[] args) throws IOException {
需求:做一界面,点打开显示文本浏览框
然后获取输入文件地址->inputfileName
再选择保存文件地址->outputfileName
TestOut tm = new TestOut();
tm.TK3(inputfileName,outputfileName);
}
}
现在就只需要界面 ,比较简单
JAVA新手,自己也正在学,但时间比较紧张,希望会的兄弟顺手解决下,谢谢
7 个解决方案
发不了图片怎么
大体就这样的:
标题:XX
输入文件位置:【输入框 】【浏览按钮】
输出文件位置:【输入框 】【浏览按钮】
【确定】 【退出】
我做了一个,自己用txt文件和jpg进行了测试,可以用,请方家指正。
给楼主个建议,使用具有可是编程能力的IDE进行GUI制作,事半功倍(我这个是用NetBeans6.1做的)
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class TestOut extends javax.swing.JFrame {
File input;
File output;
public TestOut() {
initComponents();
input = null;
output = null;
}
private void initComponents() {
jlInput = new javax.swing.JLabel();
jlOutput = new javax.swing.JLabel();
jtfInput = new javax.swing.JTextField();
jtfOutput = new javax.swing.JTextField();
jbInput = new javax.swing.JButton();
jbOutput = new javax.swing.JButton();
jbOk = new javax.swing.JButton();
jbExit = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("输入输出测试");
setResizable(false);
jlInput.setText("输入文件位置:");
jlOutput.setText("输出文件位置:");
jbInput.setText("浏览");
jbInput.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbInputActionPerformed(evt);
}
});
jbOutput.setText("浏览");
jbOutput.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbOutputActionPerformed(evt);
}
});
jbOk.setText("确定");
jbOk.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbOkActionPerformed(evt);
}
});
jbExit.setText("退出");
jbExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbExitActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addGap(64, 64, 64)
.addComponent(jbOk)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jbExit))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jlOutput, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jlInput, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jtfOutput, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jtfInput, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jbOutput)
.addComponent(jbInput))
.addContainerGap(30, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(jbInput)
.addGap(21, 21, 21)
.addComponent(jbOutput))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jtfInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jlInput))
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jtfOutput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jlOutput))))
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbExit)
.addComponent(jbOk))
.addContainerGap(31, Short.MAX_VALUE))
);
pack();
}
private void jbInputActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jfc = new JFileChooser();
int res = jfc.showOpenDialog(this);
if (res == JFileChooser.APPROVE_OPTION) {
input = jfc.getSelectedFile();
this.jtfInput.setText(input.getAbsolutePath());
}
}
//转下贴
//接上帖
private void jbExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jbOutputActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jfc = new JFileChooser();
int res = jfc.showOpenDialog(this);
if (res == JFileChooser.APPROVE_OPTION) {
output = jfc.getSelectedFile();
this.jtfOutput.setText(output.getAbsolutePath());
}
}
private void jbOkActionPerformed(java.awt.event.ActionEvent evt) {
String strInput = jtfInput.getText();
if (strInput.equals("") && input == null) {
JOptionPane.showMessageDialog(this, "输入文件错误", "请输入或者选择输入文件!", JOptionPane.WARNING_MESSAGE);
return;
} else {
File f = new File(strInput);
if (f.exists()) {
input = f;
} else {
JOptionPane.showMessageDialog(this, "输入文件不存在,请输入或者选择输入文件!", "输入文件错误", JOptionPane.ERROR_MESSAGE);
return;
}
}
String strOutput = jtfOutput.getText();
if (strOutput.equals("")) {
JOptionPane.showMessageDialog(this, "输出文件错误", "请输入或者选择输出文件!", JOptionPane.WARNING_MESSAGE);
return;
} else {
File f = new File(strOutput);
output = f;
}
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(input));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output));
byte[] buf = new byte[1024];
int readLength = bis.read(buf, 0, 1024);
while (readLength != -1) {
bos.write(buf,0,1024);
readLength = bis.read(buf, 0, 1024);
}
bos.flush();
bos.close();
bis.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(TestOut.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(TestOut.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestOut().setVisible(true);
}
});
}
private javax.swing.JButton jbExit;
private javax.swing.JButton jbInput;
private javax.swing.JButton jbOk;
private javax.swing.JButton jbOutput;
private javax.swing.JLabel jlInput;
private javax.swing.JLabel jlOutput;
private javax.swing.JTextField jtfInput;
private javax.swing.JTextField jtfOutput;
}
其实这个东西问题还是很多的:
1、从程序设计的角度说,我把功能实现和界面做在了一起,这是不好的;
2、操作过程,尤其是功能实现以后没有任何提示,这很不符合人机交互友好的要求;
3、等等
请高手多多指点。
强烈建议楼主看看《Java核心编程》第一卷,网上应该也有下载的