Datei kopieren

Dogge

Aktives Mitglied
Grüß;( Gott.Ich möchte, dass nach einer Pfadangabe(zu einer Datei) in der Konsole , diese kopiert wird.
Die kopierte Datei sollte sich dort befinden, wo auch schon die Ursprungsdatei war/ist.
Java:
package jav01E.io;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringBufferInputStream;
import java.nio.channels.FileChannel;

public class DateiKopierer {

	/**
	 * @param args
	 * @param s 
	 */
	public static void main(String[] args) {
		System.out.println("Dateipfad eingeben:");// TODO Auto-generated method stub
	      
		InputStream in = System.in;
		InputStreamReader inReader = new InputStreamReader(in);
		BufferedReader bufReader =new BufferedReader(inReader);
		
			try {
				String s = bufReader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
			
			 File inF = new File(s);
		     File outF = new File(s + ".bak");
		     
		     FileChannel inChannel = new FileInputStream(inF).getChannel();
		     FileChannel outChannel = new FileOutputStream(outF).getChannel();
		        try { 
		            inChannel.transferTo(0, inChannel.size(), outChannel);
		        } catch (IOException e) {
		            throw e; 
		        }
	}
}
Die Variable s, unter der ich den Pfad speichere, macht Probleme.(s cannot be resolved, Zeile 38)
Keine Ahnung was falsch läuft. s wurde als String deklariert. Da dürfte es keinen Fehler geben?!?????:L
 
Zuletzt bearbeitet:

XHelp

Top Contributor
Deine Variable ist nur innerhalb des try-catch-Blockes sichtbar, wenn du die außerhalb verwenden möchtest, so muss du die auch außerhalb deklarieren.
 

VfL_Freak

Top Contributor
[EDIT]too late ;([/EDIT]

Moin,

die Variable 's' ist nur im Try-Teil bekannt :
Java:
try 
{
    String s = bufReader.readLine();
}

Deklariere sie vorher !

Gruß
Klaus
 

Dogge

Aktives Mitglied
Danke. Hab String s; an den Anfang der main-Methode gepackt. Jetzt sind überall bei Eclipse Fehlermeldungen. Eclipse verlangt String s = null;
???? Was soll ich damit? Ich will doch unter s meinen Pfad.
 
S

SlaterB

Gast
und ein try/ catch ist eben dafür da, dass readLine() auch schiefgehen kann, nichts zurückgibt,
was steht dann in s? soll dein Computer explodieren oder hälst du dich lieber an die Regeln? ;)
 

Solor

Aktives Mitglied
machs doch irgendwie so:
[Java]
try
{
String s = bufReader.readLine();
//was du damit machen willst und außerhalb des blocks steht
}
catch(IOException e)
{
e.printStackTrace();
System.out.println("Ungültige Eingabe");
}
[/Java]
 

Dogge

Aktives Mitglied
Ok. Habs verschtanden. Das Compilieren hat geklappt. Aber mein Programm funktioniert trotzdem nicht.
Hier die Fehlermeldung:
java.io.FileNotFoundException: C:\eclipse_workspace\EclipseHowTo (Zugriff verweigert)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at jav01E.io.DateiKopierer.main(DateiKopierer.java:44)
java.lang.NullPointerException
at jav01E.io.DateiKopierer.main(DateiKopierer.java:57)

Java:
/*
 * @(#)FileInputStream.java	1.60 03/01/23
 *
 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.io;

import java.nio.channels.FileChannel;
import sun.nio.ch.FileChannelImpl;


/**
 * A <code>FileInputStream</code> obtains input bytes
 * from a file in a file system. What files
 * are  available depends on the host environment.
 *
 * <p><code>FileInputStream</code> is meant for reading streams of raw bytes
 * such as image data. For reading streams of characters, consider using
 * <code>FileReader</code>.
 *
 * @author  Arthur van Hoff
 * @version 1.60, 01/23/03
 * @see     java.io.File
 * @see     java.io.FileDescriptor
 * @see	    java.io.FileOutputStream
 * @since   JDK1.0
 */
public
class FileInputStream extends InputStream
{
    /* File Descriptor - handle to the open file */
    private FileDescriptor fd;

    private FileChannel channel = null;

    /**
     * Creates a <code>FileInputStream</code> by
     * opening a connection to an actual file,
     * the file named by the path name <code>name</code>
     * in the file system.  A new <code>FileDescriptor</code>
     * object is created to represent this file
     * connection.
     * <p>
     * First, if there is a security
     * manager, its <code>checkRead</code> method
     * is called with the <code>name</code> argument
     * as its argument.
     * <p>
     * If the named file does not exist, is a directory rather than a regular
     * file, or for some other reason cannot be opened for reading then a
     * <code>FileNotFoundException</code> is thrown.
     *
     * @param      name   the system-dependent file name.
     * @exception  FileNotFoundException  if the file does not exist,
     *                   is a directory rather than a regular file,
     *                   or for some other reason cannot be opened for
     *                   reading.
     * @exception  SecurityException      if a security manager exists and its
     *               <code>checkRead</code> method denies read access
     *               to the file.
     * @see        java.lang.SecurityManager#checkRead(java.lang.String)
     */
    public FileInputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null);
    }

    /**
     * Creates a <code>FileInputStream</code> by
     * opening a connection to an actual file,
     * the file named by the <code>File</code>
     * object <code>file</code> in the file system.
     * A new <code>FileDescriptor</code> object
     * is created to represent this file connection.
     * <p>
     * First, if there is a security manager,
     * its <code>checkRead</code> method  is called
     * with the path represented by the <code>file</code>
     * argument as its argument.
     * <p>
     * If the named file does not exist, is a directory rather than a regular
     * file, or for some other reason cannot be opened for reading then a
     * <code>FileNotFoundException</code> is thrown.
     *
     * @param      file   the file to be opened for reading.
     * @exception  FileNotFoundException  if the file does not exist,
     *                   is a directory rather than a regular file,
     *                   or for some other reason cannot be opened for
     *                   reading.
     * @exception  SecurityException      if a security manager exists and its
     *               <code>checkRead</code> method denies read access to the file.
     * @see        java.io.File#getPath()
     * @see        java.lang.SecurityManager#checkRead(java.lang.String)
     */
    public FileInputStream(File file) throws FileNotFoundException {
	String name = (file != null ? file.getPath() : null);
	SecurityManager security = System.getSecurityManager();
	if (security != null) {
	    security.checkRead(name);
	}
        if (name == null) {
            throw new NullPointerException();
        }
	fd = new FileDescriptor();
	open(name);
    }

    /**
     * Creates a <code>FileInputStream</code> by using the file descriptor
     * <code>fdObj</code>, which represents an existing connection to an
     * actual file in the file system.
     * <p>
     * If there is a security manager, its <code>checkRead</code> method is
     * called with the file descriptor <code>fdObj</code> as its argument to
     * see if it's ok to read the file descriptor. If read access is denied
     * to the file descriptor a <code>SecurityException</code> is thrown.
     * <p>
     * If <code>fdObj</code> is null then a <code>NullPointerException</code>
     * is thrown.
     *
     * @param      fdObj   the file descriptor to be opened for reading.
     * @throws     SecurityException      if a security manager exists and its
     *                 <code>checkRead</code> method denies read access to the
     *                 file descriptor.
     * @see        SecurityManager#checkRead(java.io.FileDescriptor)
     */
    public FileInputStream(FileDescriptor fdObj) {
	SecurityManager security = System.getSecurityManager();
	if (fdObj == null) {
	    throw new NullPointerException();
	}
	if (security != null) {
	    security.checkRead(fdObj);
	}
	fd = fdObj;
    }

    /**
     * Opens the specified file for reading.
     * @param name the name of the file
     */
    private native void open(String name) throws FileNotFoundException;

    /**
     * Reads a byte of data from this input stream. This method blocks
     * if no input is yet available.
     *
     * @return     the next byte of data, or <code>-1</code> if the end of the
     *             file is reached.
     * @exception  IOException  if an I/O error occurs.
     */
    public native int read() throws IOException;


    /**
     * Reads a subarray as a sequence of bytes.
     * @param b the data to be written
     * @param off the start offset in the data
     * @param len the number of bytes that are written
     * @exception IOException If an I/O error has occurred.
     */
    private native int readBytes(byte b[], int off, int len) throws IOException;

    /**
     * Reads up to <code>b.length</code> bytes of data from this input
     * stream into an array of bytes. This method blocks until some input
     * is available.
     *
     * @param      b   the buffer into which the data is read.
     * @return     the total number of bytes read into the buffer, or
     *             <code>-1</code> if there is no more data because the end of
     *             the file has been reached.
     * @exception  IOException  if an I/O error occurs.
     */
    public int read(byte b[]) throws IOException {
	return readBytes(b, 0, b.length);
    }

    /**
     * Reads up to <code>len</code> bytes of data from this input stream
     * into an array of bytes. This method blocks until some input is
     * available.
     *
     * @param      b     the buffer into which the data is read.
     * @param      off   the start offset of the data.
     * @param      len   the maximum number of bytes read.
     * @return     the total number of bytes read into the buffer, or
     *             <code>-1</code> if there is no more data because the end of
     *             the file has been reached.
     * @exception  IOException  if an I/O error occurs.
     */
    public int read(byte b[], int off, int len) throws IOException {
	return readBytes(b, off, len);
    }

    /**
     * Skips over and discards <code>n</code> bytes of data from the
     * input stream. The <code>skip</code> method may, for a variety of
     * reasons, end up skipping over some smaller number of bytes,
     * possibly <code>0</code>. The actual number of bytes skipped is returned.
     *
     * @param      n   the number of bytes to be skipped.
     * @return     the actual number of bytes skipped.
     * @exception  IOException  if an I/O error occurs.
     */
    public native long skip(long n) throws IOException;

    /**
     * Returns the number of bytes that can be read from this file input
     * stream without blocking.
     *
     * @return     the number of bytes that can be read from this file input
     *             stream without blocking.
     * @exception  IOException  if an I/O error occurs.
     */
    public native int available() throws IOException;

    /**
     * Closes this file input stream and releases any system resources
     * associated with the stream.
     *
     * <p> If this stream has an associated channel then the channel is closed
     * as well.
     *
     * @exception  IOException  if an I/O error occurs.
     *
     * @revised 1.4
     * @spec JSR-51
     */
    public void close() throws IOException {
        if (channel != null)
            channel.close();
        close0();
    }

    /**
     * Returns the <code>FileDescriptor</code>
     * object  that represents the connection to
     * the actual file in the file system being
     * used by this <code>FileInputStream</code>.
     *
     * @return     the file descriptor object associated with this stream.
     * @exception  IOException  if an I/O error occurs.
     * @see        java.io.FileDescriptor
     */
    public final FileDescriptor getFD() throws IOException {
	if (fd != null) return fd;
	throw new IOException();
    }

    /**
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
     * object associated with this file input stream.
     *
     * <p> The initial {@link java.nio.channels.FileChannel#position()
     * </code>position<code>} of the returned channel will be equal to the
     * number of bytes read from the file so far.  Reading bytes from this
     * stream will increment the channel's position.  Changing the channel's
     * position, either explicitly or by reading, will change this stream's
     * file position.
     *
     * @return  the file channel associated with this file input stream
     *
     * @since 1.4
     * @spec JSR-51
     */
    public FileChannel getChannel() {
	synchronized (this) {
	    if (channel == null)
		channel = FileChannelImpl.open(fd, true, false, this);
	    return channel;
	}
    }

    private static native void initIDs();

    private native void close0() throws IOException;

    static {
	initIDs();
    }

    /**
     * Ensures that the <code>close</code> method of this file input stream is
     * called when there are no more references to it.
     *
     * @exception  IOException  if an I/O error occurs.
     * @see        java.io.FileInputStream#close()
     */
    protected void finalize() throws IOException {
	if (fd != null) {
	    if (fd != fd.in) {
		close();
	    }
	}
    }
}
 

X5-599

Top Contributor
Ich rate mal so ins blaue und sage: VIelleicht liegts an Win 7 bzw. fehlende(nicht ausreichende) Zugriffsrechte? Versuche deinen Code mal mit einer Datei die im Eigene Dokumente Ordner liegt, oder so. Irgendwo wo du Schreibrechte hast.
 

Dogge

Aktives Mitglied
:eek:Eclipse hat meinen kompletten code verändert. Jetzt kann ich nichts mehr erkennen.

Ich rate mal so ins blaue und sage: VIelleicht liegts an Win 7 bzw. fehlende(nicht ausreichende) Zugriffsrechte? Versuche deinen Code mal mit einer Datei die im Eigene Dokumente Ordner liegt, oder so. Irgendwo wo du Schreibrechte hast.
Werd ich versuchen.Gute Idee.
 

Solor

Aktives Mitglied
dann exportiers als runnable jar-file und starte es in dem eigene-dateien ordner mithilfe einer batch-datei

verdammt - zu langsam
 

X5-599

Top Contributor
Andere mögliche Ursache: Ist "C:\eclipse_workspace\EclipseHowTo" wirklich der komplette Pfad zur Datei die du kopieren willst? Oder wurde da der Rest abgeschnitten?
 

Dogge

Aktives Mitglied
X5-599 Andere mögliche Ursache: Ist "C:\eclipse_workspace\EclipseHowTo" wirklich der komplette Pfad zur Datei die du kopieren willst? Oder wurde da der Rest abgeschnitten?

Hast richtigen Riecher! Da gehört noch\howto.txt dazu.Danke.
Es klappt jetzt.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
S Image Datei selektieren und in Projekt Verzeichnis abspeichern/kopieren Java Basics - Anfänger-Themen 16
M Input/Output Datei in Laufzeit-JAR kopieren Java Basics - Anfänger-Themen 6
C Datei identisch Kopieren Java Basics - Anfänger-Themen 3
J Klassen Fehler Datei kopieren - was mache ich falsch Java Basics - Anfänger-Themen 19
M Kopieren einer .wav Datei Java Basics - Anfänger-Themen 6
M Java-Datei in Ordner Kopieren Java Basics - Anfänger-Themen 12
R Datei kopieren: Performance erhöhen Java Basics - Anfänger-Themen 10
R Kopieren einer Datei Java Basics - Anfänger-Themen 18
L Datei kopieren Java Basics - Anfänger-Themen 11
C Datei kopieren - Erweiterung java.io.File Java Basics - Anfänger-Themen 2
B .class datei kopieren und auführen Java Basics - Anfänger-Themen 8
H Datei kopieren per Eingabe Java Basics - Anfänger-Themen 11
H Datei in den Windows-Programmordner kopieren Java Basics - Anfänger-Themen 5
N Binär Datei kopieren Java Basics - Anfänger-Themen 2
L Datei kopieren in anders verzeichnis geht nicht Java Basics - Anfänger-Themen 17
F Datei in Autostart kopieren Java Basics - Anfänger-Themen 2
K kopieren einer Datei Java Basics - Anfänger-Themen 10
A Per FTP Datei kopieren? Java Basics - Anfänger-Themen 11
G Datei kopieren Java Basics - Anfänger-Themen 2
Noar Datei effizient kopieren Java Basics - Anfänger-Themen 18
T Datei kopieren? Java Basics - Anfänger-Themen 7
R Datei kopieren und umbenennen Java Basics - Anfänger-Themen 14
G Inhalt von Datei in andere Datei kopieren Java Basics - Anfänger-Themen 5
K Warum wird hier nur etwas in eine txt Datei geschrieben und nicht in alle drei (InputStream/OutputStream/Reader/Writer) Java Basics - Anfänger-Themen 1
farbenlos Csv Datei in Java einlesen Java Basics - Anfänger-Themen 18
E Audio Datei unter Bedingungen ausführen Java Basics - Anfänger-Themen 19
S Daten aus Import Datei auslesen und sortieren Java Basics - Anfänger-Themen 2
A exe Datei erstellen Java Basics - Anfänger-Themen 8
J .jar datei öffnen funktioniert nicht Java Basics - Anfänger-Themen 17
P Aus Text Datei nur Zahlen übernehmen Java Basics - Anfänger-Themen 13
P Welches SDK für das erstellen einer ausführbaren Datei? Java Basics - Anfänger-Themen 4
W Fehler in der Datei pom.xml Java Basics - Anfänger-Themen 19
M Verständnisfrage: Warum wird die Datei ohne Inhalt übertragen Java Basics - Anfänger-Themen 3
D Jar Datei startet unter Linux nicht Java Basics - Anfänger-Themen 3
P Probleme mit NetBeans: Wie lässt sich jar. Datei an einem MacBook öffnen Java Basics - Anfänger-Themen 21
N Programm Funktioniert mit .txt Datei aber nicht mit .rtf Datei Java Basics - Anfänger-Themen 2
A Wie führe ich eine Batch-Datei von meiner Java-Anwendung aus? Java Basics - Anfänger-Themen 18
D Java Programm mit Batch-Datei starten Java Basics - Anfänger-Themen 32
W Objekte einer ArrayList in txt-datei schreiben mit Paths? Java Basics - Anfänger-Themen 2
E TIF Datei auslesen Java Basics - Anfänger-Themen 2
B von Java/Eclipse verwendete Datei existiert gar nicht? Java Basics - Anfänger-Themen 6
M Spezifischen Wert einer Zeile aus .txt Datei entnehmen Java Basics - Anfänger-Themen 15
B Popups mit Klicksabfangen zumAusfüllen einer .ods Datei Java Basics - Anfänger-Themen 0
M Daten aus .txt Datei einlesen und weiterverarbeiten Java Basics - Anfänger-Themen 80
M RandomAccessFile int und String gleichzeitig in einer Datei Java Basics - Anfänger-Themen 49
I Datei (Bild) Drucken und wie Druckeinstellung speichern? Java Basics - Anfänger-Themen 3
A CSV-Datei Verarbeiten Java Basics - Anfänger-Themen 8
D Downloadfortschritt von Datei über Google Drive API v3 Java Basics - Anfänger-Themen 10
A CSv.Datei einlesen und die werte in zweidemosional Int Array speichern Java Basics - Anfänger-Themen 9
B Den Dateipfad einer Java Datei durch Code in Selbiger finden? Java Basics - Anfänger-Themen 10
S In Datei schreiben in Java? Java Basics - Anfänger-Themen 1
Saiko Zeilen einer Datei einlesen Java Basics - Anfänger-Themen 3
sserio TXT-Datei Auslesen und den Wert jedes Namen ausrechnen etc. Java Basics - Anfänger-Themen 37
sserio Txt Datei einlesen Java Basics - Anfänger-Themen 9
T Printwriter Datei nicht überschreiben Java Basics - Anfänger-Themen 10
berserkerdq2 An selbst ersteller txt Datei immer Text dranhängen, ohne den vorherign Text zu löschen Java Basics - Anfänger-Themen 8
berserkerdq2 Wie gebe ich den Pfad zu einer Datei an, die in einem Ordner in Eclipse ist? Java Basics - Anfänger-Themen 1
D Strings aus Excel-Datei einlesen Java Basics - Anfänger-Themen 2
M Text in Datei schreiben Java Basics - Anfänger-Themen 9
S Datei anlegen Problem! Groß- und Kleinschreibung wird nicht unterschieden Java Basics - Anfänger-Themen 4
J selbst erstellte Datei mit Programm öffnen Java Basics - Anfänger-Themen 10
J int innerhalb einer Datei ändern Java Basics - Anfänger-Themen 1
T208 Text Datei individuell benennen. Java Basics - Anfänger-Themen 5
julian112 Input/Output .gz bzw. .txt Datei Einlesen und Umgang mit Exceptions Java Basics - Anfänger-Themen 1
F Aus eingelesener Datei korrekt Objekte erzeugen Java Basics - Anfänger-Themen 5
E extern Datei von meinem Computer aufmachen Java Basics - Anfänger-Themen 5
H Scripte oder Programmcode aus Datei lesen? Java Basics - Anfänger-Themen 5
E PDF Datei im xfdf-Datei umwandeln und auf dem Laufwerk ablegen Java Basics - Anfänger-Themen 0
J CSV-Datei verarbeiten Java Basics - Anfänger-Themen 27
A Verarbeiten einer Excel Datei durch das java-Programm Java Basics - Anfänger-Themen 3
P Datei einlesen, nach Begriff filtern und in Datei ausgeben. Problem Standardausgabe über Konsole Java Basics - Anfänger-Themen 19
nbergmann Installation unter jdk.java.net: Keine ZIP-Datei zum entpacken Java Basics - Anfänger-Themen 2
J Datei aus Netzwerk auslesen Java Basics - Anfänger-Themen 9
EchtKeineAhnungManchmal hallo habe ein Problem mit einer Datei -> (Zugriff verweigert) Java Basics - Anfänger-Themen 4
EchtKeineAhnungManchmal Controller aus FXML Datei entfernen Java Basics - Anfänger-Themen 49
I Probleme mit OutputStream - Datei lässt sich nicht öffnen Java Basics - Anfänger-Themen 4
Kotelettklopfer Sqlite DB aus Java Datei ansprechen. Java Basics - Anfänger-Themen 147
C XML Datei speichern und laden Java Basics - Anfänger-Themen 18
M Von einem Menü Methode aus anderer Klasse ausführen, die errechnete Werte in Datei schreibt. Java Basics - Anfänger-Themen 8
C XML Datei schreiben Java Basics - Anfänger-Themen 14
S Zufällige ungerade Zeile aus Text-Datei lesen Java Basics - Anfänger-Themen 5
J Wert in einer json Datei ändern und speichern Java Basics - Anfänger-Themen 3
L Java erstellt leere Datei Java Basics - Anfänger-Themen 8
J Json Datei auslesen Java Basics - Anfänger-Themen 4
J In main() Datei geöffnet, von anderer Funktion beschreiben Java Basics - Anfänger-Themen 3
I JAX-WS.... Datei ".ical" zurückgeben.... Wie annotieren? Java Basics - Anfänger-Themen 1
O zweidimensionales array in eine csv-Datei Java Basics - Anfänger-Themen 1
CptK Richtigen Pfad beim einlesen von Datei finden Java Basics - Anfänger-Themen 2
E Pfadangaben bei Ausführbarer Jar Datei Java Basics - Anfänger-Themen 8
J Input/Output Konstruktor ergänzen, der zur Datei mit einem Objekt passt Java Basics - Anfänger-Themen 0
I Datei als Stream aus Ressource laden? Java Basics - Anfänger-Themen 2
LetsSebi Methode, die einen arry von objekten speichert in einer datei Java Basics - Anfänger-Themen 6
R Wie installiere ich Jdownloadersetup.sh datei mit debian Java Basics - Anfänger-Themen 2
J Csv-Datei einlesen Java Basics - Anfänger-Themen 52
H Daten aus einer Datei in eine Liste speichern Java Basics - Anfänger-Themen 23
W Java in Exe Datei umgewandelt, Ressourcen fehlen (Bilder und Audiodateien) Java Basics - Anfänger-Themen 1
A Fehler beim Ausführen einer class Datei Java Basics - Anfänger-Themen 6
L Konstruktor für eine Map-Datei/Map-Datei einlesen Java Basics - Anfänger-Themen 5
S CSV Datei auslesen und anders darstellen Java Basics - Anfänger-Themen 2
O zufälliges Wort aus einer Datei einlesen Java Basics - Anfänger-Themen 32

Ähnliche Java Themen

Neue Themen


Oben