po0ya_jo0n
سه شنبه 07 اردیبهشت 1389, 06:53 صبح
میخام از فایل Sam ویندوز در آدرس زیر :
C:\Windows\System32\config
یه کپی بگیرم مثلا روی فلش مموری و یا هر جا. یا توی JTextField نشون بده
وولی متأسفانه با این سورس نتونستم. ایراد کار کجاست؟
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class form1 extends JFrame implements ActionListener{
JTextArea ta1=new JTextArea(10,50);
JScrollPane sp1=new JScrollPane(ta1);
JLabel l1=new JLabel("File Name:");
JTextField t1=new JTextField(40);
JButton b1=new JButton("Save");
JButton b2=new JButton("Open");
form1(){
getContentPane().setLayout(new FlowLayout());
getContentPane().add(sp1);
getContentPane().add(l1);
getContentPane().add(t1);
getContentPane().add(b1);
getContentPane().add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==b1){
try{
//define a file to write/save
FileOutputStream file1=new FileOutputStream(t1.getText());
String fc=ta1.getText();
//save to file--convert string to array of bytes through getBytes() method
file1.write(fc.getBytes());
file1.close();
JOptionPane.showMessageDialog(this,"Saved");
}
catch(Exception e1){
JOptionPane.showMessageDialog(this,"File write error");
}
}
if(ae.getSource()==b2){
try{
//define a file to read/open
FileInputStream file2=new FileInputStream(t1.getText());
//find file size and save it into fsize variable
int fsize=file2.available();
//define an array of bytes
byte[] buffer=new byte[fsize];
//read file contents and save it into buffer variable
file2.readline(buffer);
file2.close();
//convert an array of bytes to String
String fc=new String(buffer);
ta1.setText(fc);
}
catch(Exception e1){
JOptionPane.showMessageDialog(this,"File read error");
}
}
}
public static void main(String args[]){
form1 frm=new form1();
frm.setSize(580,400);
frm.setLocation(100,100);
frm.setVisible(true);
}
}
با تشکر
C:\Windows\System32\config
یه کپی بگیرم مثلا روی فلش مموری و یا هر جا. یا توی JTextField نشون بده
وولی متأسفانه با این سورس نتونستم. ایراد کار کجاست؟
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class form1 extends JFrame implements ActionListener{
JTextArea ta1=new JTextArea(10,50);
JScrollPane sp1=new JScrollPane(ta1);
JLabel l1=new JLabel("File Name:");
JTextField t1=new JTextField(40);
JButton b1=new JButton("Save");
JButton b2=new JButton("Open");
form1(){
getContentPane().setLayout(new FlowLayout());
getContentPane().add(sp1);
getContentPane().add(l1);
getContentPane().add(t1);
getContentPane().add(b1);
getContentPane().add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==b1){
try{
//define a file to write/save
FileOutputStream file1=new FileOutputStream(t1.getText());
String fc=ta1.getText();
//save to file--convert string to array of bytes through getBytes() method
file1.write(fc.getBytes());
file1.close();
JOptionPane.showMessageDialog(this,"Saved");
}
catch(Exception e1){
JOptionPane.showMessageDialog(this,"File write error");
}
}
if(ae.getSource()==b2){
try{
//define a file to read/open
FileInputStream file2=new FileInputStream(t1.getText());
//find file size and save it into fsize variable
int fsize=file2.available();
//define an array of bytes
byte[] buffer=new byte[fsize];
//read file contents and save it into buffer variable
file2.readline(buffer);
file2.close();
//convert an array of bytes to String
String fc=new String(buffer);
ta1.setText(fc);
}
catch(Exception e1){
JOptionPane.showMessageDialog(this,"File read error");
}
}
}
public static void main(String args[]){
form1 frm=new form1();
frm.setSize(580,400);
frm.setLocation(100,100);
frm.setVisible(true);
}
}
با تشکر