could be made to sync time though :unsure:
i started teaching myself java then errr stopped :(
Printable View
could be made to sync time though :unsure:
i started teaching myself java then errr stopped :(
Sample output
i broke mine, Windows won't ever have the precision of 10 seconds again :(Quote:
Originally Posted by TheDave
Yeah, I suppose it could be modified to sync the time. I've been learning java for about a year and a half now, but they don't teach us much to do with interaction with windows (and I can't be fecked learning :no: )Quote:
Originally Posted by TheDave
i wish they taught it in pill form, that would be cool :01:
The blue pill or the red pill. :ph34r:Quote:
Originally Posted by TheDave
A FileMoving program I made. It takes about 50-75% of the time windows takes to move the same file (which is quite surprising considering how slow java is in comparison to 'real' languages)Code:import java.io.*;
public class FileMover
{
public static void main (String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the filename you wish to move: ");
String fileToBeMoved = in.readLine();
File file = new File (fileToBeMoved);
while(!file.exists())
{
System.out.println("The file you've entered does not exist.\nPlease try again: ");
fileToBeMoved = in.readLine();
file = new File (fileToBeMoved);
}
long fileLength = file.length();
System.out.print("Enter the destination path: ");
String destpath = in.readLine();
File dir = new File(destpath);
String pathname = file.getAbsolutePath();
System.out.println("\nYou are about to move: \n\n"+pathname+"\nto \n"+destpath);
System.out.print("\nIf you're happy with this, enter 'y' to proceed.\nOtherwise press 'n' to cancel: ");
char userInput = (char) System.in.read();
if(userInput == 'y')
{
double start = System.currentTimeMillis();
boolean fine = file.renameTo(new File(dir, file.getName()));
double end = System.currentTimeMillis();
double timeTaken = (end - start)/1000;
if(fine)
{
if(timeTaken > 0)
{
double transferRate = ((fileLength/1024)/timeTaken);
System.out.println("\nFile "+fileToBeMoved+" is "+fileLength+" bytes and took "+timeTaken+" seconds to move");
System.out.println("Average transfer rate was "+Math.round(transferRate)+" kB/sec");
}
else
System.out.println("\nFile moved successfully");
}
else
System.out.println("\nFile not moved");
}
else
System.out.println("\nYou have cancelled the move\n");
}
}
Commented version of above (if anyone wants to learn anything from it :lol:Code:import java.io.*;
public class FileMover
{
public static void main (String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// Set up BufferedReader to take user input
System.out.print("Enter the filename you wish to move: ");
String fileToBeMoved = in.readLine();
// Read in the pathname from a user of the file they wish to move
File file = new File (fileToBeMoved);
// Create a new file object with the path from above
while(!file.exists()) // While they don't enter a real file
{
System.out.println("The file you've entered does not exist.\nPlease try again: ");
// Tell them they didn't enter a real file
fileToBeMoved = in.readLine();
// Read in a new pathname
file = new File (fileToBeMoved);
// Create new file object...
}
long fileLength = file.length();
// Get length of the file to be moved
System.out.print("Enter the destination path: ");
String destpath = in.readLine();
// Read in destination path from user
File dir = new File(destpath);
// Create a new using the destination path from the user
String pathname = file.getAbsolutePath();
// Get the full path of the file the user inputs (e.g. C:\Windows\System32\winmine.exe)
System.out.println("\nYou are about to move: \n\n"+pathname+"\nto \n"+destpath);
System.out.print("\nIf you're happy with this, enter 'y' to proceed.\nOtherwise press 'n' to cancel: ");
char userInput = (char) System.in.read();
// Read in one char from the user
if(userInput == 'y') // If the user enters a 'y' (i.e. they want to move the file)
{
double start = System.currentTimeMillis();
// Get current system time (to be used in calculation later
boolean fine = file.renameTo(new File(dir, file.getName()));
// Move the file (by renaming the path)
// Set the boolean value "fine" to true if the transfer goes OK, otherwise set it to false
double end = System.currentTimeMillis();
// Get current system time (after the move operation)
double timeTaken = (end - start)/1000;
// Calculate how long it took to move the file (division by 1000 gives result in seconds)
if(fine) // If the file move went OK
{
if(timeTaken > 0) // If it took more than "0" time to move (to counter possible division by 0)
{
double transferRate = ((fileLength/1024)/timeTaken);
// Calculate transfer rate for the file
System.out.println("\nFile "+fileToBeMoved+" is "+fileLength+" bytes and took "+timeTaken+" seconds to move");
System.out.println("Average transfer rate was "+Math.round(transferRate)+" kB/sec");
}
else // If it didn't take more than "0" seconds
System.out.println("\nFile moved successfully");
}
else // If "fine" is false (i.e. if there was a problem with the move
System.out.println("\nFile not moved");
}
else // If the user cancelled the transfer (by entering other than 'y' above)
System.out.println("\nYou have cancelled the move\n");
}
}
Sample run (obviously it's simple with no real GUI):
The only reason that java is slower than 'real' languages is because it has to be interpreted first. If that is a relatively small part of the procedure compared to the actual work done there is no reason why it shouldn't work almost as fast.Quote:
Originally Posted by Tifosi
Your claim that it is faster than windows doing the same file move is almost certainly wrong. When windows moves a file, it has actually finished moving it when it says it has finished. In contrast the java program hands that task over to windows. Windows will have cached some of the move and allowed the java program to continue. Windows will still be moving the file while your program is waiting for you to "Press any key to continue...".
:P
The gov time server is the atomic one not Microsloth
http://img75.exs.cx/img75/7362/time5.jpg