程序设计(1)设计一个Application程序,将键盘输入的内容保存在文件My.txt中。
         (2)设计一个Applet程序,利用一个线程来控制一个实心圆球(直径为10)在窗口中随机移动。
         (3)编写一个Application程序,使用URL类和字节输入流将网络资源保存倒本地计算机中的文件c:/windows/index.html中。谢谢各位高手了 。。急用  帮帮初学者吧

解决方案 »

  1.   

    题一:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;public class WriteTxt { /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args){
    FileOutputStream fos=null;
    try {
    fos=new FileOutputStream(new File("F:\\my.txt"));
    PrintWriter pw=new PrintWriter(fos);
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String input;
    while(!(input=br.readLine()).equals("q")){ //输入q程序结束
    pw.println(input);
    }
    pw.flush();
    pw.close();
    }catch (IOException e) {
    e.printStackTrace();
    }finally{
    try {
    fos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    }}
      

  2.   


    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;import javax.swing.JOptionPane;public class FileWriter { public static void main(String[] args) {

    String filename="d:/My.txt";
    File aFile=new File(filename);
    String str=JOptionPane.showInputDialog("Please input your words:");
    byte buffer[]=str.getBytes();

    try {
    FileOutputStream file1=new FileOutputStream(aFile);
    file1.write(buffer);
    file1.close();
    } catch (IOException e) {
    System.out.println(e);
    }

    }}
      

  3.   

    题二:import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;public class DrawCircularity extends Applet implements Runnable{ private int x=0,y=0;  //鼠标的位置
    private boolean move=false;
    private Object sync;
    @Override
    public void init() {
    setSize(200,200);
    sync=this;
    addMouseMotionListener(new MouseMotionListener(){
    public void mouseDragged(MouseEvent e) {
    }
    public void mouseMoved(MouseEvent e) {
    x=e.getX();
    y=e.getY();
    synchronized(sync){
    move=true;
    sync.notify();
    }
    }});
    new Thread(this).start();
    }
    public void run() {
    while(true){
    if(move){
    repaint();
    move=false;
    }else{
    synchronized (sync){
    try {
    sync.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    }
    }
    @Override
    public void paint(Graphics g) {
    g.setColor(Color.red);
    g.fillOval(x, y, 10,10);
    }



    }线程中使用的下同步,是因为发现没有同步cup的要到100%
      

  4.   

    package temporary;import java.io.*;
    import java.net.URL;public class WriteHtml { /**
     * @param args
     */

    public static String gethtml(String addr){
    try 
    {  
    URL url = new URL(addr);  
    BufferedReader br = new BufferedReader(new InputStreamReader(url  
    .openStream()));  
    String html = "";  
    StringBuffer sb = new StringBuffer("");  
    while ((html = br.readLine()) != null) {  
    sb.append(html + "\r\n");  
    }  
    br.close();  
    return sb.toString();  
    } catch (Exception e) {  
    return addr + "is error url";  
    }
    }

    public static boolean writeHtml(String url){
    StringBuffer buffer = new StringBuffer();
    buffer.append(gethtml(url));
    byte[] buffered = buffer.toString().getBytes();
    BufferedOutputStream pw = null;
     try {
    pw = new BufferedOutputStream(new FileOutputStream("test.html"));
    pw.write(buffered, 0,buffered.length);
    pw.close();
    return true;
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return false;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return false;
    }
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    if(WriteHtml.writeHtml("http://www.baidu.com"))
    System.out.print("保存成功");

    }}