前天寫了一篇技術博客把之前自己寫的Java I/O讀寫文件的代碼上傳了,但後來想想並不妥。東西是能夠用了,但是並不是真正意義上的產品。所以今天給前天的不足加上了一些代碼,把文件讀寫真正做成一個產品,一個傻瓜式的誰都能用的產品。
在這個文件讀寫的產品上我加上了UI(用戶界面):
雖然界面醜了點,但也算是一個入門的產品了。下面上代碼:
首先是UI:
package com.czj_IO;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class JavaIOUI extends JFrame {
public static void main(String[] args) {
JavaIOUI jui = new JavaIOUI();
jui.init();
}
/**
* 初始化界面的方法
*/
private void init() {
this.setTitle("文件讀寫");
this.setSize(500, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setLayout(new FlowLayout());
addBackground();
JLabel j1 = new JLabel(" 文件名 : ");
JTextArea jp1 = new JTextArea(2, 30);
jp1.setText("這是文件讀寫實例.txt");
JLabel j2 = new JLabel("文件絕對路徑:");
JTextArea jp2 = new JTextArea(2, 30);
jp2.setText("E://");
JLabel j3 = new JLabel(" 內容 : ");
JTextArea jp3 = new JTextArea(5, 30);
jp3.setText("這是文件讀寫實例的文件內容。");
this.add(j1);
this.add(jp1);
this.add(j2);
this.add(jp2);
this.add(j3);
this.add(jp3);
JButton jb1 = new JButton("寫入文件");
jb1.setBorderPainted(false);
jb1.setFocusable(false);
this.add(jb1);
JButton jb2 = new JButton("讀取文件");
jb2.setBorderPainted(false);
jb2.setFocusable(false);
this.add(jb2);
JLabel j4 = new JLabel("(必須已有該文件)");
this.add(j4);
JLabel j5 = new JLabel();
this.add(j5);
//給按鈕添加監聽器
IOListener il = new IOListener(j5,jp1,jp2,jp3,jb1,jb2);
jb1.addActionListener(il);
jb2.addActionListener(il);
this.setVisible(true);
}
/**
* 添加背景的方法
*/
private void addBackground(){
ImageIcon image=new ImageIcon(this.getClass().getResource("背景1.jpg"));
JLabel jlaImage=new JLabel(image);
jlaImage.setBounds(0, -35, image.getIconWidth(), image.getIconHeight());
this.getLayeredPane().add(jlaImage,new Integer(Integer.MIN_VALUE));
JPanel contentpane=(JPanel)this.getContentPane();
contentpane.setOpaque(false);
}
}
接下來是監聽器:
package com.czj_IO;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class IOListener implements ActionListener{
private JTextArea jp1,jp2,jp3;
private JButton jb1,jb2;
private JLabel j5;
public IOListener(JLabel j5, JTextArea jp1,JTextArea jp2,JTextArea jp3, JButton jb1, JButton jb2){
this.jp1 = jp1;
this.jp2 = jp2;
this.jp3 = jp3;
this.jb1 = jb1;
this.jb2 = jb2;
this.j5 = j5;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(jb1.getText())){
TestIO ti = new TestIO();
try {
ti.writeFile(jp3.getText(),jp2.getText()+jp1.getText());
j5.setText("文件成功寫入!");
} catch (Exception e1) {
e1.printStackTrace();
}
// System.out.println(jp3.getText());
// System.out.println(jp2.getText()+jp1.getText());
}else if(e.getActionCommand().equals(jb2.getText())){
TestIO ti = new TestIO();
ti.readFile(jp2.getText()+jp1.getText());
j5.setText("文件成功讀出!已在控制檯輸出!");
}
}
}
再接下來就是上篇博客裏已經有的主代碼了:
package com.czj_IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class TestIO {
/**
* 讀文件的方法
* @param fName文件絕對路徑
*/
public void readFile(String fName){
try {
FileInputStream fis = new FileInputStream(fName);
int n=fis.read();//讀取下一個字節
//循環讀寫
while(n!=-1){
System.out.println("讀到的字節是"+n);
n = fis.read();
}
fis.close();//關閉輸入流
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 寫文件的方法
* @param msg寫入的內容
* @param s文件絕對路徑
* @throws Exception拋出異常
*/
public void writeFile(String msg,String s) throws Exception{
try {
FileOutputStream fos = new FileOutputStream(s,false);
byte[] b = msg.getBytes();//得到組成字符串的字節
fos.write(b);
fos.close();//關閉輸出流
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TestIO tjm = new TestIO();
tjm.readFile("E://類與對象總結.txt");
try {//此語句運行兩次必須改寫入文件的文件名或刪除上次寫入的文件,否則報錯(已有該文件)
tjm.writeFile("hello! 你好!~~~~", "E://寫入的文件1.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
}
有些時候別人並不在意你寫了多少代碼,只在意你通過代碼實現了什麼功能。而要提現你所實現的東西就必須把這個東西做成可視可用的產品。
梣梓cenzi
2015-0506