Results 1 to 3 of 3
Thread: how to save file..
- 02-12-2009, 06:15 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 1
- Rep Power
- 0
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.");
}
}
- 02-12-2009, 07:10 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
check out FileWriter and see where that leads you.
- 02-12-2009, 11:33 PM #3
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
-
Read file from URL and save to FTP
By nitinmukesh in forum NetworkingReplies: 6Last Post: 06-03-2012, 08:45 PM -
upload and save file
By chennee72 in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 06-29-2011, 02:27 PM -
File dialog with save
By manpreet in forum New To JavaReplies: 1Last Post: 01-02-2009, 05:44 PM -
[SOLVED] Save an xml file
By mvg in forum XMLReplies: 8Last Post: 10-13-2008, 12:25 PM -
save file to database
By katerinaaa in forum New To JavaReplies: 0Last Post: 08-14-2007, 12:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks