Results 1 to 3 of 3
  1. #1
    jont717 is offline Member
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Default how to save file..

    Hi, I am new to Java and to this forum.

    Just wondering what code I would need to insert into this source code to save the file to disk. Here is the code.

    import java.net.*;
    import java.io.*;
    import java.util.Scanner;

    public class URLTimer {

    static Scanner s = new Scanner(System.in); // connect Scanner s to the keyboard

    public static void main(String[] args) throws Exception {

    String site; // will hold URL
    System.out.print("\n\nEnter URL of site to test: "); // don't enter a bad one!
    site = "http" + s.next();

    URL url = new URL(site);

    long startTime = System.currentTimeMillis();

    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    url.openStream()));

    long connectTime = System.currentTimeMillis(); // return from TCP handshake

    String inputLine;
    int charCount = 0;

    while ((inputLine = in.readLine()) != null) {
    charCount += inputLine.length();
    System.out.println(inputLine);
    }

    long endTime = System.currentTimeMillis(); // all data displayed

    in.close();

    System.out.println("\n\nResponse Time Measurements for " + site);
    System.out.println("Connection time: " + (connectTime - startTime) + " msec.");
    System.out.println("Page Transfer/Display time: " + (endTime - connectTime) + " msec.");
    System.out.println(" (" + charCount + " characters @ " +
    (int)((double)(charCount / (endTime - connectTime))) + " chars/msec.)"); // ugly?
    System.out.println("Total time: " + (endTime - startTime) + " msec.");

    }
    }

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    check out FileWriter and see where that leads you.

  3. #3
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Smile A useful filewriting method

    Here's a method I found that makes a .txt file. If the fileDest(File Destination) already exists, it overwrites:
    Java Code:
    public void createScrambledFile(String fileDest) throws IOException{
         FileWriter fw = new FileWriter(fileDest);
         BufferedWriter bw = new BufferedWriter(fw);
         bw.write("The String to put in your new .txt file\nblahblah\nHello" +
                      " World JAVA from the File!");
         bw.close();
    }
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. Read file from URL and save to FTP
    By nitinmukesh in forum Networking
    Replies: 6
    Last Post: 06-03-2012, 08:45 PM
  2. upload and save file
    By chennee72 in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 06-29-2011, 02:27 PM
  3. File dialog with save
    By manpreet in forum New To Java
    Replies: 1
    Last Post: 01-02-2009, 05:44 PM
  4. [SOLVED] Save an xml file
    By mvg in forum XML
    Replies: 8
    Last Post: 10-13-2008, 12:25 PM
  5. save file to database
    By katerinaaa in forum New To Java
    Replies: 0
    Last Post: 08-14-2007, 12:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •