Ghi tới File bởi sử dụng Applet trong Java



Miêu tả vấn đề

Cách ghi một file bởi sử dụng Applet trong Java?

Giải pháp

Ví dụ sau minh họa cách ghi một file bằng việc tạo textarea để write trong một trình duyệt bởi sử dụng TextArea() tạo Label và sau đó sử dụng File() constructor để tạo File trong Java.

Quảng cáo
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import java.net.*;

public class WriteFile extends Applet{
   Button write = new Button("WriteToFile");
   Label label1 = new Label("Enter the file name:");
   TextField text = new TextField(20);
   Label label2 = new Label("Write your text:");
   TextArea area = new TextArea(10,20);
   public void init(){
      add(label1);
      label1.setBackground(Color.lightGray);
      add(text);
      add(label2);
      label2.setBackground(Color.lightGray);
      add(area);
      add(write,BorderLayout.CENTER);
      write.addActionListener(new ActionListener (){
        public void actionPerformed(ActionEvent e){
           new WriteText();
        }
     }
   );
}
public class WriteText {
   WriteText(){
      try {
         String str = text.getText();
         if(str.equals("")){
            JOptionPane.showMessageDialog(null,
            "Please enter the file name!");
            text.requestFocus();
         }
         else{
            File f = new File(str);
            if(f.exists()){
               BufferedWriter out = new 
               BufferedWriter(new FileWriter(f,true));
               if(area.getText().equals("")){
                  JOptionPane.showMessageDialog
                  (null,"Please enter your text!");
                  area.requestFocus();
               }
               else{
                  out.write(area.getText());
                  if(f.canWrite()){
                     JOptionPane.showMessageDialog(null,
                     "Text is written in "+str);
                     text.setText("");
                     area.setText("");
                     text.requestFocus();
                  }
                  else{
                     JOptionPane.showMessageDialog(null,
                     "Text isn't written in "+str);
                  }
                  out.close();
                  }
               }
               else{
                  JOptionPane.showMessageDialog
                  (null,"File not found!");
                  text.setText("");
                  text.requestFocus();
               }
            }
         }
         catch(Exception x){
            x.printStackTrace();
         }
      }
   }
}
Quảng cáo

Kết quả

Code trên sẽ cho kết quả sau trong trình duyệt java-enabled.

View in Browser. 

Đã có app VietJack trên điện thoại, giải bài tập SGK, SBT Soạn văn, Văn mẫu, Thi online, Bài giảng....miễn phí. Tải ngay ứng dụng trên Android và iOS.

Theo dõi chúng tôi miễn phí trên mạng xã hội facebook và youtube:

Các bạn có thể mua thêm khóa học JAVA CORE ONLINE VÀ ỨNG DỤNG cực hay, giúp các bạn vượt qua các dự án trên trường và đi thực tập Java. Khóa học có giá chỉ 300K, nhằm ưu đãi, tạo điều kiện cho sinh viên cho thể mua khóa học.

Nội dung khóa học gồm 16 chuơng và 100 video cực hay, học trực tiếp tại https://www.udemy.com/tu-tin-di-lam-voi-kien-thuc-ve-java-core-toan-tap/ Bạn nào có nhu cầu mua, inbox trực tiếp a Tuyền, cựu sinh viên Bách Khoa K53, fb: https://www.facebook.com/tuyen.vietjack

Follow facebook cá nhân Nguyễn Thanh Tuyền https://www.facebook.com/tuyen.vietjack để tiếp tục theo dõi các loạt bài mới nhất về Java,C,C++,Javascript,HTML,Python,Database,Mobile.... mới nhất của chúng tôi.


applet_trong_java.jsp


Tài liệu giáo viên