Ich versuche, einen Wagenrücklauf aus einer Java-Zeichenfolge zu entfernen, aber bisher nicht viel Glück. Hier ist, was ich derzeit Code-weise habe. Sie können sich die gewünschten Strings ansehen, die unten benötigt werden, verglichen mit dem, was ich bekomme.Zeilenumbruch von String entfernen funktioniert nicht
public static void main(String[] args) {
String tempString = "";
String tempStringTwo = "";
String [] convertedLines = new String [100];
int counter = 0;
int tempcount = 0;
File file = new File("input.txt");
if (!file.exists()) {
System.out.println(args[0] + " does not exist.");
return;
}
if (!(file.isFile() && file.canRead())) {
System.out.println(file.getName() + " cannot be read from.");
return;
}
try {
FileInputStream fis = new FileInputStream(file);
char current;
while (fis.available() > 0) { // Filesystem still available to read bytes from file
current = (char) fis.read();
tempString = tempString + current;
int character = (int) current;
if (character==13) { // found a line break in the file, need to ignore it.
//tempString = tempString.replace("\n"," ").replace("\r"," ");
tempString = tempString.replaceAll("\\r|\\n", " ");
//tempString = tempString.substring(0,tempString.length()-1);
//System.out.println(tempString);
}
if (character==46) { // found a period
//System.out.println("Found a period.");
convertedLines[counter] = tempString;
tempString = "";
counter++;
tempcount = counter;
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("------------------------------------------------");
for (int z=0;z<tempcount;z++){
System.out.println(convertedLines[z]);
}
}
Hier ist meine aktuellen Ausgabe ...
The quick brown fox
jumps over the lazy dogs.
Now is the time for all good men to come to the
aid of their country.
All your base are
belong to us.
Hier ist, was ich brauche ...
The quick brown fox jumps over the lazy dogs.
Now is the time for all good men to come to the aid of their country.
All your base are belong to us.
Hinweis: Überprüfen Sie nicht 'fis.available()> 0', um EOF zu erkennen. Überprüfen Sie den int-Wert von 'fis.read()' und brechen Sie, wenn es '<0' ist. –
Außerdem: es ist nicht nötig, 'current' zu werfen, um es' character' zuzuweisen; und keine Notwendigkeit für "character", da Sie "current" direkt verwenden können; und keine Notwendigkeit, "13" und "46" zu verwenden, stattdessen "" und "" verwenden. –
Warum sollten Sie nach "13" suchen? Warum nicht einfach 'convertedLines [Zähler] = tempString.replaceAll (" \\ r | \\ n "," ");' im Block "gefundene Periode"? –