I'm using Java and I need to append, concat, merge or add, whichever is the right term, two .rtf files together in its original format for both rtf files, into one rtf file. Each of the rtf files are a page long, so I need to create a two page rtf file, from the two files.
I also need to create a page break between the two files, in the new combined rtf file. I went to MS word and was able to combine the two rtf files together, but that just created a long rtf file with no page break.
I have a code, but it only copies one file to another file in the same way, but I need help on tweaking out this code so I can copy two files into one file
FileInputStream file = new FileInputStream("old.rtf");
FileOutputStream out = new FileOutputStream("new.rtf");
byte[] buffer = new byte[1024];
int count;
while ((count= file.read(buffer)) > 0)
out.write(buffer, 0, count);
How do I add another FileInputStream object on top of the FileInputStream file, into FileOutputStream out, with a page break between file and object?
I am totally stuck. I was able to combine the two rtf files with help, but could not keep the original format of the two rtf file into the new one.
I tried the :
FileInputStream file = new FileInputStream("old.rtf");
FileOutputStream out = new FileOutputStream("new.rtf", true);
byte[] buffer = new byte[1024];
int count;
while ((count= file.read(buffer)) > 0)
out.write(buffer, 0, count);
FileOutputStream(File file, boolean append), where the old.rtf is suppose to append to new.rtf, but when I do it, the old.rtf is just written into the new.rtf.
What am I doing wrong?
When you open the file you wish to add to, use the FileOutputStream(File file, boolean append) with append set to true, then you can add to the new file, rather than over write it.
FileInputStream file = new FileInputStream("old.rtf");
FileOutputStream out = new FileOutputStream("new.rtf", true);
byte[] buffer = new byte[1024];
int count;
while ((count= file.read(buffer)) > 0)
out.write(buffer, 0, count);
This will append old.rtf to new.rtf.
You can also do:
FileInputStream file = new FileInputStream("old1.rtf");
FileOutputStream out = new FileOutputStream("new.rtf");
byte[] buffer = new byte[1024];
int count;
while ((count= file.read(buffer)) > 0)
out.write(buffer, 0, count);
file.close();
file = new FileOutputStream("old2.rtf");
while ((count= file.read(buffer)) > 0)
out.write(buffer, 0, count);
That will concatenate old1.rtf and old2.rtf to the new file new.rtf.
Related
I tried to create a file, write to it and then turn the file into an input stream and transfer its bytes to the output stream of the HTTP response. But I get the message "/tmp/mozilla_xxxx/33JJ1OHw.md.part could not be saved, because the source file could not be read." when testing it.
Here's the code that does this part.
f = new File("f.md");
f.createNewFile();
fw = new FileWriter(f);
fw.append("#" + query + "\n" + queryResult);
fw.close();
resp.setContentType("text/markdown");
OutputStream out = resp.getOutputStream();
FileInputStream in = new FileInputStream(f);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
As you can see in the documentation, File class is not meant to read the actual file content, it is just...
An abstract representation of file and directory pathnames.
But, there are many ways of getting file's content, just use one of the following classes: FileReader, BufferedReader, Scanner and Files.
Here you'll see different examples to do that, just use the one you find better. Different ways of Reading a text file in Java
try
{
FileInputStream fis=new FileInputStream(new File("Binary.txt"));
byte[] infoBin=new byte[fis.available()];
fis.read(infoBin);
for (byte b : infoBin)
{
String bin=Integer.toBinaryString(b);
}
}
How to read a file and convert that file contents into binary then write the binary to a new file using java
After Binary conversion, i don't know how to write the string bin into the new file ?
//reading from file
byte[] array = Files.readAllBytes(Paths.get("Binary.txt"));
//saving to file
FileOutputStream fos = new FileOutputStream("Binary.txt");
fos.write(array );
fos.close();
How to read a file and convert that file contents into binary
They already are binary.
then write the binary to a new file using java
There's no need to waste memory, or assume the file fits into memory, or assume the file size fits into an int. Memorise the following loop for copying between streams in Java:
int count;
byte[] buffer = new byte[8192];
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
I have a list of files from different locations. I create a zip file using the following the code which works without error. But when I try to unzip the file in Windows using Extract All it fails seeing unable to find any bytes, yet if I double click into the zip file itself with Windows Explorer I can see the files and individual ones can be opened and contains the correct data
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
for (File next : files)
{
ZipEntry zipEntry = new ZipEntry(next.getName());
zos.putNextEntry(zipEntry);
FileInputStream in = new FileInputStream(next);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
zos.close();
This may or may not be related but I've found using fixed byte length can lead to a loss of new line characters.
This may help:
final byte[] newLine = System.getProperty(
"line.separator").getBytes("UTF-8");
while ((line = in.readLine()) != null)
final byte[] buffer = line.getBytes("UTF-8");
out.write(buffer, 0, buffer.length);
out.write(newLine, 0, newLine.length);
}
A web Service expects a byte[] coming from a zip file.
I have some files in a folder that I zip with Java and then I get the byte[] from this zip file.
Is this necessary or can I create the byte[] straight from the folder?
I think something like this would allow you to do what you want without writing as long as the files are not going to be very big.
String[] sourceFiles = { "C:/file1", "C:/file2" };
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zout = new ZipOutputStream(baos);
byte[] buffer = new byte[4096];
for (int i = 0; i < sourceFiles.length; i++)
{
FileInputStream fin = new FileInputStream(sourceFiles[i]);
zout.putNextEntry(new ZipEntry(sourceFiles[i]));
int length;
while ((length = fin.read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
zout.closeEntry();
fin.close();
}
zout.close();
byte[] bytes = baos.toByteArray();
A folder is a collection of files. It is a container. It does not have a byte stream to get in the first place.
On the other hand, a ZIP (or any archive) is a file. The information about the different files is stored within the ZIP file itself
However, you can iterate through the folder contents, cook up a byte array and then use it (you are doing that anyways while creating the ZIP).
I would like to copy my image file by name From E:\Tejas\FM_Operations\source to E:\Tejas\FM_Operations\destination in a such way that if I call the MovePhoto(source,destination,filename) method, then my image will copied to the destination folder in Java.
You might want to consider using FileUtils.copyFile() from Apache Commons IO. Otherwise you'll have to copy the bytes manually, like this:
InputStream in = new FileInputStream(new File("/path/to/src"));
OutputStream out = new FileOutputStream(new File("/path/to/dest"));
byte[] buffer = new byte[8192];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
in.close();
out.close();