Datei download, verschieben und umbennen -> Welche Fehler

T

t-bone-hd-990d

Hallo,

heute hab ich mein erstes Programm zusammengebastelt.

Hab nur 10 Fehler *grins*

Was war mein Fehler bzw. wie wird die txt Datei richtig verschoben

Könnt ihr euch meine Code ansehen, da ich leider totaler Anfänger bin und lieber VB programmiere.

Code:

/**
*
*
*
*
* @********
*
*/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.ByteChannel;
import java.nio.channels.FileChannel;




//Datei runterladen

URL bild = new URL("http://webserver.de/shfjhwiue.txt");
InputStream in = bild.openStream();

byte[] buffer = new byte[8192];

FileOutputStream out = new FileOutputStream(new File("shfjhwiue.txt"));

int _tmp = 0;

while((_tmp = stream.read(buffer)) > 0){
out.write(buffer, 0 , _tmp);
}

//Danach Datei verschieben in "C:\USERNAME\Test\Unterordner\"

public class FileCopy {
long chunckSizeInBytes;
boolean verbose;
public FileCopy(){
this.chunckSizeInBytes = 1024 * 1024; //Standard: Buffer 1MB
this.verbose = false; //Statistics about Copy Process
}
public FileCopy(boolean verbose){
this.chunckSizeInBytes = 1024 * 1024; //Standard: Buffer 1MB
this.verbose = verbose; //Statistics about Copy Process
}
public FileCopy(long chunckSizeInBytes){
this.chunckSizeInBytes = chunckSizeInBytes; //Custom Buffer (Bytes)
this.verbose = false; //Statistics about Copy Process
}
public FileCopy(long chunckSizeInBytes, boolean verbose){
this.chunckSizeInBytes = chunckSizeInBytes; //Custom Buffer (Bytes)
this.verbose = verbose; //Statistics about Copy Process
}
public void copy(File source, File destination) {
try {
FileInputStream fileInputStream = new FileInputStream(source);
FileOutputStream fileOutputStream = new FileOutputStream(destination);
FileChannel inputChannel = fileInputStream.getChannel();
FileChannel outputChannel = fileOutputStream.getChannel();
transfer(inputChannel, outputChannel, source.length(), false);
fileInputStream.close();
fileOutputStream.close();
destination.setLastModified(source.lastModified());
} catch (Exception e) {
e.printStackTrace();
}
}
public void transfer(FileChannel fileChannel, ByteChannel byteChannel, long lengthInBytes, boolean verbose)
throws IOException {
long overallBytesTransfered = 0L;
long time = -System.currentTimeMillis();
while (overallBytesTransfered < lengthInBytes) {
long bytesTransfered = 0L;
bytesTransfered = fileChannel.transferTo(overallBytesTransfered, Math.min(chunckSizeInBytes, lengthInBytes - overallBytesTransfered), byteChannel);
overallBytesTransfered += bytesTransfered;
if (verbose) {
System.out.println("overall bytes transfered: " + overallBytesTransfered + " progress " + (Math.round(overallBytesTransfered / ((double) lengthInBytes) * 100.0)) + "%");
}
}
time += System.currentTimeMillis();
if (verbose) {
System.out.println("Transfered: " + overallBytesTransfered + " bytes in: " + (time / 1000) + " s -> " + (overallBytesTransfered / 1024.0) / (time / 1000.0) + " kbytes/s");
}
}
}

// Datei umbennen

File source = new File("shfjhwiue.txt");
File destination = new File("shfjhwiue.exe");
(!source.renameTo(destination)){
}


out.close();
 
Moin,

wenn Du die Code-Tags benutzen würdest (#-Button im Editor resp. den Code mit [_JAVA_][/_JAVA_] ohne die Unterstriche klammerst) und vor allem mal beschreibst, wo welchen Fehler Du redest, hättest Du auch eine Chance, dass sich das mal jemand hier anschaute :eek:;)

So ist es reine Rätselraterei :p

Gruß
Klaus
 
Meine Fehleranzeige:

--------------------Configuration: 123 - JDK version 1.6.0_17 <Default> - <Default>--------------------
C:\Users\Christoph\Documents\JCreator Pro\MyProjects\KRS\123\src\123.java:22: class, interface, or enum expected
URL bild = new URL("http://webserver.de/shfjhwiue.txt");
^
C:\Users\Christoph\Documents\JCreator Pro\MyProjects\KRS\123\src\123.java:23: class, interface, or enum expected
InputStream in = bild.openStream();
^
C:\Users\Christoph\Documents\JCreator Pro\MyProjects\KRS\123\src\123.java:25: class, interface, or enum expected
byte[] buffer = new byte[8192];
^
C:\Users\Christoph\Documents\JCreator Pro\MyProjects\KRS\123\src\123.java:27: class, interface, or enum expected
FileOutputStream out = new FileOutputStream(new File("shfjhwiue.txt"));
^
C:\Users\Christoph\Documents\JCreator Pro\MyProjects\KRS\123\src\123.java:29: class, interface, or enum expected
int _tmp = 0;
^
C:\Users\Christoph\Documents\JCreator Pro\MyProjects\KRS\123\src\123.java:31: class, interface, or enum expected
while((_tmp = stream.read(buffer)) > 0){
^
C:\Users\Christoph\Documents\JCreator Pro\MyProjects\KRS\123\src\123.java:33: class, interface, or enum expected
}
^
C:\Users\Christoph\Documents\JCreator Pro\MyProjects\KRS\123\src\123.java:90: class, interface, or enum expected
File source = new File("shfjhwiue.txt");
^
C:\Users\Christoph\Documents\JCreator Pro\MyProjects\KRS\123\src\123.java:91: class, interface, or enum expected
File destination = new File("shfjhwiue.exe");
^
C:\Users\Christoph\Documents\JCreator Pro\MyProjects\KRS\123\src\123.java:92: class, interface, or enum expected
(!source.renameTo(destination)){
^
10 errors

Process completed.



und


--------------------Configuration: 123 - JDK version 1.6.0_17 <Default> - <Default>--------------------
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)

where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.

-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -jre-no-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
show splash screen with specified image

Process completed.


Hilft euch das weiter?

Gruß
 
Yeep - tut es :)

Gesetzt den Fall, dass DIES wirklich Dein Codeanfang ist:

Java:
/**
 * 
 *
 * 
 *
 * @********
 * 
 */
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.ByteChannel;
import java.nio.channels.FileChannel;




//Datei runterladen

URL bild = new URL("http://webserver.de/shfjhwiue.txt");
InputStream in = bild.openStream();

byte[] buffer = new byte[8192];

FileOutputStream out = new FileOutputStream(new File("shfjhwiue.txt"));

int _tmp = 0;			        			      
			     
while((_tmp = stream.read(buffer)) > 0){
	out.write(buffer, 0 , _tmp);
}

//Danach Datei verschieben in "C:\USERNAME\Test\Unterordner\"

public class FileCopy {
long chunckSizeInBytes;
...........

dann kann es auch nicht klappen, da - wie Kai008 schon schrieb - hier noch einiges im Code fehlt!
Alles nach Deinen Imports MUSS in einer Hauptklasse für die Anwendung eingebettet !
Das hängt so alles im luftleeren Raum ....

Gruß
Klaus
 
Zurück