Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Neue Zeile in Textdatei einfügen

Empfohlene Antworten

Veröffentlicht

Hi Leute,

ich bin auf der Suche nach einer Möglichkeit in eine Textdatei eine neue Zeile einzufügen.

Hintergrund ist folgender, ich habe ein Log-File welches regelmäßig durch neue Einträge aktualisiert wird. Diese neuen Einträge sollen immer an der gleichen Stelle eingefügt werden, so das alle alten Einträge nach unten verschoben werden.

Über ein RandomAccessFile werden die alten Einträge leider überschrieben, es sei denn ich lade das komplette Log-File in ein Array und füge die neuen dann hinzu. Allerdings dauert diese Methode mit zunehmender Dateigröße sehr lange.

Kann mir das jemand nen ratsamen Tipp geben?

Danke

  • 2 Wochen später...

Ich kann mein Beitrag nicht editieren :confused:

...egal.

So nun, wenn du ein Buffer benutzt, dann soll es angeblich schneller gehen, hier ist mein Vorschlag:


public class Tester {


	public static void main(String[] args) {

		String message = new Date() + " - test test\r\n";

		File f = new File("logfile.log");

		File temp = new File("temp.log");


		try {

			if (!f.exists()) {

				f.createNewFile();

			}

			byte[] buffer = new byte[1024];

			FileInputStream fileIn = new FileInputStream(f);

			FileOutputStream out = new FileOutputStream(temp);


			out.write(message.getBytes());


			int len = 0;

			while ((len = fileIn.read(buffer)) > 0) {

				out.write(buffer, 0, len);

			}

			fileIn.close();

			out.close();

			f.delete();


			temp.renameTo(f);

		} catch (IOException e1) {

			// TODO Auto-generated catch block

			e1.printStackTrace();

		}

	}

}

Buffergröße proportional zur Dateigröße.

Viel Glück :)

Gruß .Erbse

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.