public class upload
{
public static void main(String[] args) throws IOException
{
PrintWriter pw = new PrintWriter("output.txt");
BufferedReader br = new BufferedReader(new FileReader("upp/400.txt"));
for (int i =0 ; i<3;i++)
{
br = new BufferedReader(new FileReader("upp/40"+Integer.toString(i)+".txt"));
String line = br.readLine();
while (line != null)
{
pw.println(line);
line = br.readLine();
}
}
pw.flush();
br.close();
pw.close();
}
}```
i want to concat / merge 3 files
400.txt
#java buffer and file reader help
30 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @sly stag! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
i want 400.txt 401.txt 402.txt to be concat / merge
it works but change the content of the files
output
48ef bfbd 6412 5608 0a00 0000 5641 6405
0a73 6175 7261 6268 2073 6175 7261 6268
680a 7361 7261 7377 6174 2030 3830 390a
expected
123
uvfdis
[]\
it works fine for me
windows
it works on online compiler
windows
solution
I guess your files have different charsets
make sure they are all in utf-8
nothing changed
public class upload
{
public static void main(String[] args) throws IOException
{
PrintWriter pw = new PrintWriter("output.txt","UTF-8");
BufferedReader br = new BufferedReader(new FileReader("upp/400.txt"));
for (int i =0 ; i<3;i++)
{
br = new BufferedReader(new FileReader("upp/40"+Integer.toString(i)+".txt"));
String line = br.readLine();
while (line != null)
{
pw.println(line);
line = br.readLine();
}
}
pw.flush();
br.close();
pw.close();
}
}```
I mean the actual files. Are their data stored in utf-8?
400.txt, 401.txt, 402.txt
try (
var in1 = Files.newInputStream("400.txt");
var in2 = Files.newInputStream("401.txt");
var in3 = Files.newInputStream("402.txt");
var out = Files.newOutputStream("out.txt");
) {
in1.transferTo(out);
in2.transferTo(out);
in3.transferTo(out);
}
this should realistically be all you need to concatenate file contents into one file
Yes, but it's not like the code they proposed wouldn't work too
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
The fact is .... Java is executing properly
The metadata of the files is creating problems
UTF-8 ......and all that stuff
You should try and be more vague