Witam jak moge wyczyścić wszystkie dane z pliku tekstowego? Myślałem, żeby zrobić metode zapisującą do pliku z opcją nadpisywania. ALE NIESTETY muszę coś tam wpisać bo inaczej nie działa.. Jak mogę to zrobić inaczej?
- Rejestracja:prawie 7 lat
- Ostatnio:ponad 6 lat
- Postów:50
0
- Rejestracja:prawie 8 lat
- Ostatnio:prawie 7 lat
- Postów:3
0
package com.cleaner;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class FileCleaner {
public static void cleanFileContent(String filePath) {
try (PrintWriter writer = new PrintWriter(filePath)) {
writer.print("");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
- Rejestracja:prawie 7 lat
- Ostatnio:ponad 6 lat
- Postów:50
0
Niestety powyższa metoda nie działa :( Dalej po wczytaniu zawartości pliku mam te same dane :/
- Rejestracja:prawie 7 lat
- Ostatnio:ponad 6 lat
- Postów:50
0
Jak mam zrobić, żeby usuwanie zawartości pliku działało?
package ćw6;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
public class FileOperation
{
static Set<String> SetNames = new HashSet<String>();
static Set<String> SetSurnames = new HashSet<String>();
static LinkedList<String> ListNames = new LinkedList<String>();
static LinkedList<String> ListSurnames = new LinkedList<String>();
public static void record(String name, String surname) throws IOException
{
PrintStream namesWriter = new PrintStream(new FileOutputStream("Names.txt", true));
PrintStream surnamesWriter = new PrintStream(new FileOutputStream("Surnames.txt", true));
namesWriter.println(name);
surnamesWriter.println(surname);
namesWriter.close();
surnamesWriter.close();
}
public static LinkedList<String> readingNames() throws FileNotFoundException, IOException
{
try
{
FileReader readNames = new FileReader("Names.txt");
BufferedReader bufferedReaderNames = new BufferedReader(readNames);
String line = bufferedReaderNames.readLine();
while (line != null)
{
SetNames.add(line);
line = bufferedReaderNames.readLine();
}
ListNames.addAll(SetNames);
bufferedReaderNames.close();
readNames.close();
} catch (FileNotFoundException e)
{
System.err.println("Nie można odnalezć pliku o nazwie \"Names.txt\"");
} catch (IOException e)
{
System.err.println("Nieznany błąd wejścia typu Input-Output");
}
return ListNames;
}
public static LinkedList<String> readingSurnames() throws FileNotFoundException, IOException
{
try
{
FileReader readSurnames = new FileReader("Surnames.txt");
BufferedReader bufferedReadSurnames = new BufferedReader(readSurnames);
String line = bufferedReadSurnames.readLine();
while (line != null)
{
SetSurnames.add(line);
line = bufferedReadSurnames.readLine();
}
ListSurnames.addAll(SetSurnames);
bufferedReadSurnames.close();
readSurnames.close();
} catch (FileNotFoundException e)
{
System.err.println("Nie można znalezć pliku o nazwie \"Surnames.txt\"");
} catch (IOException e)
{
System.err.println("Nieznany błąd wejścia typu Input-Output");
}
return ListSurnames;
}
public static void cleanMyFile() throws FileNotFoundException
{
PrintWriter writerNames = new PrintWriter("Names.txt");
PrintWriter writerSurnames = new PrintWriter("Surnames.txt");
writerNames.print("");
writerSurnames.print("");
writerNames.close();
writerSurnames.close();
}
}
- Rejestracja:prawie 7 lat
- Ostatnio:ponad 6 lat
- Postów:50
0
Tak problem rozwiązany problem był z niewyczyszczonym setem xD
cleanMyFile()
?