Problem mit FileOutputStream

  • Themenstarter Themenstarter JJay-JJay
  • Beginndatum Beginndatum
J

JJay-JJay

Gast
Guten Tag.

Ich finde das Problem nicht. Wäre lieb, wenn sich das jemand mal anschauen könnte. Ich bin noch Anfänger.

Das ist der Programmteil, um den es geht:

Java:
void pb_einlesen_for_MouseClicked() {

		btEinlesen.addActionListener(this);

		setTitle("Einlesen und Verarbeiten");
		setSize(650, 440);
		setVisible(true);
		// ein shortcut zum Schließen
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public void actionPerformed(ActionEvent e) {
		fd1.setVisible(true);
		lab1.setText("Directory: " + fd1.getDirectory());
		display(fd1.getDirectory() + fd1.getFile());
	}

	public void display(String fname) { // Pfad auslesen
		try {
			FileInputStream fis1 = new FileInputStream(fname);
			int fileSize = fis1.available();
			byte buf1[] = new byte[fileSize];
			fis1.read(buf1);
			String str1 = new String(buf1);
			ta1.setText(str1);
		} catch (IOException e) {
			System.exit(0);
		}
		
		System.out.println("fname " + fname);
		fpath = fname;
		System.out.println("fpath " + fpath);

		try {
			Process p = Runtime.getRuntime().exec(
					"rundll32 url.dll,FileProtocolHandler " + fname);
		} catch (Exception err) {
			err.printStackTrace();
		}
	}

	// Formatieren und Rechnen
	void pb_verarbeiten_for_MouseClicked() {
		HSSFWorkbook wb = null;
		FileInputStream input = null;
		try {
			input = new FileInputStream(fpath);
			System.out.println("Pfad angekommen.");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("fpath " + fpath);
			System.out.println("Pfad nicht gefunden.");
			Fehler fail = new Fehler();
			e.printStackTrace(); 
		}
		try {
			wb = new org.apache.poi.hssf.usermodel.HSSFWorkbook(input);
			System.out.println("Es funktioniert.");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Pfad konnte nicht erfolgreich übernommen werden.");
			e.printStackTrace();
		}
		
	    HSSFSheet sheet = wb.getSheetAt(0);
	    HSSFRow row = sheet.getRow(2);
	    HSSFCell cell = row.getCell((short) 3);
	    if (cell == null)
	        cell = row.createCell((short) 3);
	    cell.setCellValue("a test");
	    System.out.println("In die Zelle geschrieben.");

	    // Write the output to a file
	    FileOutputStream fileOut = null;
		try {
			fileOut = new FileOutputStream(fpath);
		} catch (FileNotFoundException e) {
			System.out.println("Fehler beim Erstellen des FileOutputStreams.");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    try {
			wb.write(fileOut);
		} catch (IOException e) {
			System.out.println("Fehler beim Schreiben in den FileOutputStream.");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    try {
			fileOut.close();
		} catch (IOException e) {
			System.out.println("Fehler beim Schließen des FileOutputStreams.");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

und das die dazugehörige Fehlermeldung:

Java:
fname C:\test\TestdateiNotizen.xls
fpath C:\test\TestdateiNotizen.xls
Pfad angekommen.
Es funktioniert.
In die Zelle geschrieben.
Fehler beim Erstellen des FileOutputStreams.
java.io.FileNotFoundException: C:\test\TestdateiNotizen.xls (The requested operation cannot be performed on a file with a user-mapped section open)
	at java.io.FileOutputStream.open(Native Method)
	at java.io.FileOutputStream.<init>(Unknown Source)
	at java.io.FileOutputStream.<init>(Unknown Source)
	at HauptfensterEditor.pb_verarbeiten_for_MouseClicked(HauptfensterEditor.java:335)
	at HauptfensterEditor$MyMouseAdapter.mouseClicked(HauptfensterEditor.java:228)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Button.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at org.apache.poi.poifs.storage.BigBlock.doWriteData(BigBlock.java:55)
	at org.apache.poi.poifs.storage.HeaderBlockWriter.writeData(HeaderBlockWriter.java:214)
	at org.apache.poi.poifs.storage.BigBlock.writeBlocks(BigBlock.java:86)
	at org.apache.poi.poifs.filesystem.POIFSFileSystem.writeFilesystem(POIFSFileSystem.java:263)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:916)
	at HauptfensterEditor.pb_verarbeiten_for_MouseClicked(HauptfensterEditor.java:342)
	at HauptfensterEditor$MyMouseAdapter.mouseClicked(HauptfensterEditor.java:228)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Button.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

Er kommt also bis zum FileOutputStream. Aber ich weiß nicht, warum es da nicht weitergeht. Kann mir jemand das Problem benennen? ^^

Vielen lieben Dank im Voraus. 😉

Gruß
JJay-JJay
 
java.io.FileNotFoundException: C:\test\TestdateiNotizen.xls (The requested operation cannot be performed on a file with a user-mapped section open)

Auf deutsch: Die Datei die du erstellen/überschreiben willst gibt es bereits und ist gerade geöffnet. Darum hast du keine Schreibrechte darauf.
 
wieso willst du denn exakt in dieselbe Datei wieder speichern?
gut, Texteditoren, Excel usw. können das, auch dein Quelltext landet in derselben Datei,
für Java und Streams ist das aber nicht so schön

entweder schließt du vorher deinen InputStream, was hoffentlich keine Konflikte mit dem Workbook gibt, oder aber du speicherst woanders,
und nachdem alles geschlossen ist kannst du ja vielleicht noch die neue Datei an die Stelle der alten schieben
(machen größere Programme vielleicht auch..)
 
Achso, ich verstehe. Danke schön. 🙂

Ich will in exakt dieselbe Datei wieder speichern, weil sie nicht nur geöffnet und gelesen, sondern von meinem Programm auch bearbeitet werden soll. Zusätzliche Zellen einfügen und rechnen usw.

Also wenn ich den Input schließe, kann ich darin speichern? Hab ich das richtig verstanden? Oder kann ich das angepasste dann als Kopie speichern oder mit neuem Namen oder sowas?

Wenn wir schon dabei sind, weiß jemand spontan den Befehl, um von einem anderen Excel Zellen in mein Input zu kopieren? Muss ich es dafür öffnen? (Den Befehl kann ich natürlich auf der Apache POI seite suchen, aber wenn es jemand spontan weiß, spart das Zeit. 🙂)

Vielen Dank nochmal und viele Grüße

JJay-JJay
 
zwei Tipps:
- lies doch einfach die Antworten, interpretiere wie es gemeint war, schreibe nicht dasselbe nochmal in Frageform 😉
- noch wichtiger: probiere diese dir nun zumindest als Möglichkeit vorstellbaren Dinge aus,
wenn was davon geht, dann wäre das doch ein Fortschritt
 

Zurück
Oben