Nobboo
15-04-2003, 23:27
em có đoạn code sau
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class sendFile extends JApplet implements ActionListener
{
JPanel panelMain;
JPanel panelSendFile;
List listName;
JButton buttonSend;
GridBagLayout gb;
GridBagConstraints gbc;
CardLayout card;
JFileChooser fileChooser = new JFileChooser();
public void init()
{
gb = new GridBagLayout();
gbc = new GridBagConstraints();
card = new CardLayout();
panelMain = new JPanel();
panelSendFile = new JPanel();
listName = new List();
buttonSend = new JButton("Send");
getContentPane().add(panelMain);
panelMain.setLayout(card);
panelMain.add("SendFile",panelSendFile);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=1;
gb.setConstraints(listName,gbc);
panelSendFile.add(listName);
gbc.gridx=1;
gbc.gridy=2;
gb.setConstraints(buttonSend,gbc);
panelSendFile.add(buttonSend);
buttonSend.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String pathSend;
String pathSave;
Object obj =e.getSource();
if(obj == buttonSend)
{
if(fileChooser.showOpenDialog(this)==0);
{
pathSend=fileChooser.getSelectedFile().getPath();
}
if(fileChooser.showSaveDialog(this)==0);
{
pathSave=fileChooser.getSelectedFile().getPath();
try
{
FileInputStream fiStream = new FileInputStream(pathSend) ;
InputStreamReader isReader = new InputStreamReader(fiStream);
BufferedReader buffer = new BufferedReader(isReader);
String line1;
while( (line1=buffer.readLine())!=null)
{
/*Opening the file, seeking the end of file, and writing the login details to the file. */
RandomAccessFile logFile1 = new RandomAccessFile ( pathSave,"rw");
logFile1.seek(logFile1.length());
logFile1.writeBytes(line1+"\n");
}
}
catch(Exception ex)
{
System.out.println("Error: " + ex);
}
}
}
}
};
/*
<HTML>
<APPLET CODE="sendFile.class"
WIDTH=680
HEIGHT=480>
</APPLET>
*/
khi bấm vào nút send thì hiện lên hộp thoại Open File sau khi bấm vào nút Open thì mở lên 1 hộp thoại Save File.
không hiểu sao no chi save chính xác các file text mà thôi, còn save File như file hình, file mp3 ... thì không được :.( ,
Xin hỏi có cách nào giải quyết được vấn đề trên không, cho em xin code luôn càng tốt :D :D :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class sendFile extends JApplet implements ActionListener
{
JPanel panelMain;
JPanel panelSendFile;
List listName;
JButton buttonSend;
GridBagLayout gb;
GridBagConstraints gbc;
CardLayout card;
JFileChooser fileChooser = new JFileChooser();
public void init()
{
gb = new GridBagLayout();
gbc = new GridBagConstraints();
card = new CardLayout();
panelMain = new JPanel();
panelSendFile = new JPanel();
listName = new List();
buttonSend = new JButton("Send");
getContentPane().add(panelMain);
panelMain.setLayout(card);
panelMain.add("SendFile",panelSendFile);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=1;
gb.setConstraints(listName,gbc);
panelSendFile.add(listName);
gbc.gridx=1;
gbc.gridy=2;
gb.setConstraints(buttonSend,gbc);
panelSendFile.add(buttonSend);
buttonSend.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String pathSend;
String pathSave;
Object obj =e.getSource();
if(obj == buttonSend)
{
if(fileChooser.showOpenDialog(this)==0);
{
pathSend=fileChooser.getSelectedFile().getPath();
}
if(fileChooser.showSaveDialog(this)==0);
{
pathSave=fileChooser.getSelectedFile().getPath();
try
{
FileInputStream fiStream = new FileInputStream(pathSend) ;
InputStreamReader isReader = new InputStreamReader(fiStream);
BufferedReader buffer = new BufferedReader(isReader);
String line1;
while( (line1=buffer.readLine())!=null)
{
/*Opening the file, seeking the end of file, and writing the login details to the file. */
RandomAccessFile logFile1 = new RandomAccessFile ( pathSave,"rw");
logFile1.seek(logFile1.length());
logFile1.writeBytes(line1+"\n");
}
}
catch(Exception ex)
{
System.out.println("Error: " + ex);
}
}
}
}
};
/*
<HTML>
<APPLET CODE="sendFile.class"
WIDTH=680
HEIGHT=480>
</APPLET>
*/
khi bấm vào nút send thì hiện lên hộp thoại Open File sau khi bấm vào nút Open thì mở lên 1 hộp thoại Save File.
không hiểu sao no chi save chính xác các file text mà thôi, còn save File như file hình, file mp3 ... thì không được :.( ,
Xin hỏi có cách nào giải quyết được vấn đề trên không, cho em xin code luôn càng tốt :D :D :