Scrieti un program Java care citeste dintr-un fisier cuvinte (un cuvant pe o linie) si
scrie intr-un fisier pe un singur rand, cu un spatiu intre cuvinte, ce a citit.
Răspunsuri la întrebare
Răspuns:
import java.io.*;
import java.util.*;
public class WordReader {
public static void main(String[] args) {
FileReader inputFile = null;
BufferedReader inputBuffer = null;
FileWriter outputFile = null;
BufferedWriter outputBuffer = null;
try {
// Open the input file
inputFile = new FileReader("input.txt");
inputBuffer = new BufferedReader(inputFile);
// Open the output file
outputFile = new FileWriter("output.txt");
outputBuffer = new BufferedWriter(outputFile);
// Read the words from the input file, one line at a time
String line = inputBuffer.readLine();
while (line != null) {
// Write the word to the output file, followed by a space
outputBuffer.write(line + " ");
// Read the next line
line = inputBuffer.readLine();
}
// Close the input file
inputBuffer.close();
inputFile.close();
// Close the output file
outputBuffer.close();
outputFile.close();
} catch (IOException e) {
// If there was an error, print a message and exit
System.out.println("There was an error reading or writing the files.");
System.exit(1);
}
}
}