How to zip long string into file and retrieve it? - java

I am trying to zip a long string into a file and to retrieve it.
The following code does not work. What it retrieves is gibberish.
public static void main(String args[]) {
try {
// Creating base for data
StringBuilder sbb = new StringBuilder();
for (int i=0;i<1000;i++)
sbb.append("ùertyty!|").append(Integer.toString(i));
File FileAll = new File(".\\All.data");
FileAll.createNewFile();
// Zipping into file
DeflaterOutputStream g = new DeflaterOutputStream(
new FileOutputStream(FileAll));
OutputStreamWriter osw = new OutputStreamWriter(g);
String base = sbb.toString();
osw.write(base);
osw.close();
FileInputStream ALL_FIS = new FileInputStream(FileAll);
// Re-reading from file
DeflaterInputStream dis = new DeflaterInputStream(ALL_FIS);
InputStreamReader isr = new InputStreamReader(dis);
StringBuilder sb = new StringBuilder();
char[] c = new char[1000];
int count = isr.read(c);
while ( count != -1 ) {
sb.append(c, 0, count);
count = isr.read(c);
}
isr.close();
String retr = sb.toString();
System.out.println("Are equal: " + retr.equals(base));
System.out.println("Base: " + base);
System.out.println("Retr: " + retr);
} catch (IOException e) {
System.err.println(e.toString());
}
}
What am I doing wrong?
P.S.: It seems like DeflaterInputStream does not do its job and returns the content of the file as is.

You need to use a InflaterInputStream (which decompresses) instead of a DeflaterInputStream
http://download.oracle.com/javase/1.5.0/docs/api/java/util/zip/InflaterInputStream.html

Related

How to split a file in java into multiple same size parts using bytestream

The following is the code I have tried. I am able to read 100KB of files
song.mp3(total size is 2.4MB), but not able to read subsequent chunks (100KB)
in the loop. The loop only creates the file song_0.mp3 and it's empty.
I need to create files as song_0.mp3, song_1.mp3,...
public class fileIOwrite2multiplefiles {
public static void main(String[] args) throws IOException{
// TODO code application logic here
File file = new File("song.mp3");
FileInputStream fIn = new FileInputStream("song.mp3");
FileOutputStream fOut = new FileOutputStream("song_0.mp3");
int chunk_size = 1024*100;
byte[] buff = new byte[chunk_size]; // 100KB file
while(fIn.read()!=-1){
fIn.read(buff);
String file_name =file.getName();
int i=1;
int total_read=0;
total_read +=chunk_size;
long read_next_chunk= total_read;
String file_name_new = file_name+"_"+ i +".mp3";
File file_new = new File(file_name);
i++;
fOut = new FileOutputStream(file_name_new);
fOut.write(buff);
buff = null;
fIn.skip(total_read);// skip the total read part
}//end of while loop
fIn.close();
fOut.close();
}
}
You can rewrite your main method as :
public static void main(String[] args) throws IOException {
File file = new File("song.mp3");
FileInputStream fIn = new FileInputStream("song.mp3");
FileOutputStream fOut = new FileOutputStream("song_0.mp3");
int chunk_size = 1024 * 100;
byte[] buff = new byte[chunk_size]; // 100KB file
int i = 0;
String file_name = file.getName();
String file_name_base = file_name.substring(0, file_name.lastIndexOf("."));
while (fIn.read() != -1) {
fIn.read(buff);
int total_read = 0;
total_read += chunk_size;
long read_next_chunk = total_read;
String file_name_new = file_name_base + "_" + i + ".mp3";
File file_new = new File(file_name_base);
i++;
fOut = new FileOutputStream(file_name_new);
fOut.write(buff);
fIn.skip(total_read);// skip the total read part
} // end of while loop
fIn.close();
fOut.close();
}
The incremental variable declaration (int i = 0) was needed to create outside the while loop.
Don't null the variable buff as it will throw Null-pointer in next iteration.
File's base name needed to be extracted in order to create the file parts with names you want. E.g. song_0.mp3, song_1.mp3,...

Java, encoding with my custom cipher

I have a program that encode and decodes with my custom cipher, text files and lossless media files, but the problem is that over 2MB it crashes.
void doTheRabi(File f, byte[] hashedPass) {
try {
// BufferedReader br = new BufferedReader(new InputStreamReader(new
// FileInputStream(f))); // legge il file
// String response = new
// String(Files.readAllBytes(Paths.get(f.getAbsolutePath()))); // scrive tutto
// il file in memoria
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String response = new String(); // ASSEGNO IL CONTENUTO DEL FILE IN QUESTA STRINGA
for (String line; (line = br.readLine()) != null; response += line + "\n")
;
response = response.replace("\n", "newline").replace("\r", "newrow"); // rimpiazzo le new line con "newline"
// e "newrow"
byte[] encodedfile = response.getBytes(StandardCharsets.UTF_8); // trasformo il file in byte
byte[] result = new byte[encodedfile.length]; // variabile temporanea
int hpc = 0;
for (int i = 0; i < result.length; i++) {
result[i] = (byte) (encodedfile[i] + hashedPass[hpc++]); // algoritmo rabi
if (hpc == hashedPass.length) {
hpc = 0;
}
}
String encodedresult = Base64.getEncoder().encodeToString(result); // restituisco il risultato in base64
FileWriter fw = new FileWriter(f);
PrintWriter pw = new PrintWriter(fw);
pw.print("");
pw.append(encodedresult /* + "extension=" + extString */); // scrivo nel file tutto il risultato
pw.flush();
pw.close();
fw.close();
br.close();
String path = f.getAbsolutePath();
String newName = path + ".rab1";
f.renameTo(new File(newName));
} catch (Exception e) {
console.appendText("Error: " + e.getMessage() + "\n");
e.printStackTrace();
}
}
// operazione inversa
void killTheRabi(File f, byte[] hashedPass) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String response = new String();
for (String line; (line = br.readLine()) != null; response += line)
;
byte[] decodedfile = Base64.getDecoder().decode(response);
byte[] result = new byte[decodedfile.length];
int hpc = 0;
for (int i = 0; i < result.length; i++) {
result[i] = (byte) (decodedfile[i] - hashedPass[hpc++]);
if (hpc == hashedPass.length) {
hpc = 0;
}
}
String resultString = bytesToString(result);
String finalres = resultString.replace("newline", "\n").replace("newrow", "\r");
FileWriter fw = new FileWriter(f);
PrintWriter pw = new PrintWriter(fw);
pw.print("");
pw.append(finalres);
pw.flush();
pw.close();
fw.close();
br.close();
String path = f.getAbsolutePath();
String newName = path.replace(".rab1", "");
f.renameTo(new File(newName));
} catch (Exception e) {
console.appendText("Error: " + e.getMessage() + "\n");
e.printStackTrace();
}
}
What am I doing wrong? I think it's because the memory gets full, since java uses a virtual machine, but I don't know a way to enhance the memory usage, maybe using buffers but am I not using them already?
Since you are possibly holding quite a bit of data in memory, try the following:
Increase maximum heap size to be used by the JVM by starting with the parameter -Xmx2048m or more

How to write number using fileoutputstream

I'm new to java , i need to define counter then write the result in a file
int counter=0;
int resultstweets=0;
fos = new FileOutputStream(new File(
prop.getProperty("PATH_TO_OUTPUT_FILE")));
BufferedReader br = new BufferedReader(new FileReader("path/of/file"));
while ((tweetJson = br.readLine()) != null) {
String result = drpc.execute(TOPOLOGY_NAME, tweetJson);
Status s = null;
try {
s = DataObjectFactory.createStatus(tweetJson);
result = s.getId() + "\t" + s.getText() + "\t" + result;
// this is my counter
resultstweets+=counter;
} catch (TwitterException e) {
LOG.error(e.toString());
}
fos.write(result.getBytes());
fos.write(newLine);
}
fos.write(newLine);
fos.write("Finish: ".getBytes());
fos.write("resultstweets".getBytes());
fos.write(newLine);
// here i write it in the file
fos.write(resultstweets);
but what i got at the end of file
Finish: resultstweets
**\001**459202139258
This method java.io.FileOutputStream.write(byte[] b) you're using in your last line gets a byte array as parameter.
So you should first convert your integer to string and then call getBytes on that:
fos.write(String.valueOf(resultstweets).getBytes());
You can find a proper example of using this method here.

How to create an file and copy the content from another file into the created file in java? [duplicate]

This question already has answers here:
Standard concise way to copy a file in Java?
(16 answers)
Closed 8 years ago.
I am trying to read a file and write to another file it is not working I invoke the method from the main
public boolean copy(String inputPlayList, String outputPlayList, int numberOfMunites)
{
String start1 = "#EXTINF:";
String afterNum = ";";
try
{
declaring those variable that I would use to pass the method
File fInput, fOutput;
String s;
String a;
assigning those variable to the method
fInput = new File(inputPlayList);
fOutput = new File(outputPlayList);
// Now I am using bufferedRead and BufferedWriter to read and write in a file
BufferedReader br = new BufferedReader(new FileReader(new File(inputPlayList)));
BufferedWriter out = new BufferedWriter(new BufferedWriter(new FileWriter(outputPlayList)));
// creating a while saying while the line is not finish contunue to read
while((s = br.readLine())!= null)
{
if(s.contains(start1)) {
String numberInString = s.substring(start1.length(), s.indexOf(afterNum));
numberOfMunites+= Integer.getInteger(numberInString);
}
// when it is finsh close the file.
out.write(s);
}
out.close();
System.out.println("donne");
}catch ( IOException e)
{
System.err.println("the is an erro that need to be fixed"+e);
}
return false;
}
}
Simplest way in java:
File input = new File("input/file");
File output = new File("output/file");
InputStream is = new FileInputStream(input); // can be any input stream, even url.open()
OutputStream os = new FileOutputStream(output);
byte[] buffer = new byte[4096];//
int read = 0;
while ((read = is.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
is.close();
os.close();
Try Apache Commons IO Utils.
FileUtils.copyFile(new File(inputPlayList),new File(outputPlayList));
Here it is, but I don't understand the meaning of the numberOfminutes argument, what is it for? I've changed implementation to return calculated number of minutes from the function.
import java.io.*;
public class Main {
public static void main(String[] args) {
System.out.println(copy("D:\\1.txt", "D:\\2.txt", 0)); //returns the calculated number of minutes
}
public static int copy(String inputPlayList, String outputPlayList, int numberOfMinutes) {
String start1 = "#EXTINF:";
String afterNum = ";";
try {
BufferedReader br = new BufferedReader(new FileReader(new File(inputPlayList)));
PrintWriter out = new PrintWriter(new FileWriter(outputPlayList));
String s;
while ((s = br.readLine()) != null) {
if (s.contains(start1)) {
String numberInString = s.substring(start1.length(), s.indexOf(afterNum));
numberOfMinutes += Integer.parseInt(numberInString);
}
out.println(s);
}
out.close();
} catch (IOException e) {
System.err.println("Exception" + e);
}
return numberOfMinutes;
}
}

Reading a large number of BitSet Objects from a file in Java

I want read a large number of BitSet objects from a file (12MB). I used following code but only read first object from file and repeated it. thanks
public static void main(String[] args) {
// TODO code application logic here
ObjectInputStream Input = null;
FileInputStream Database = null;
Object Buffer = null;
BitSet H = null;
try
{
Database = new FileInputStream("BloomFilters.txt");
Input = new ObjectInputStream(Database);
while((Buffer = Input.readObject()) != null)
{
H = (BitSet)Buffer;
System.out.println(H);
System.out.println("Yes" );
}
}
catch(Exception e)
{
System.out.println("Exp = " + e.getMessage());
}
and following code create a file of BitSet objects, I want read objects from this file
public class Main {
public static void main(String[] args) {
BloomFilter Set = new BloomFilter(512, 100);
ObjectOutputStream Output = null;
DataInputStream Input = null;
FileOutputStream DBOut = null;
FileInputStream DBIn = null;
String Sequence = "";
try
{
DBOut = new FileOutputStream("Bloomfilters.txt");
Output = new ObjectOutputStream(DBOut);
DBIn = new FileInputStream("DB.txt");
Input = new DataInputStream(DBIn);
while((Sequence = (String) Input.readLine()) != null)
{
Set.clear();
for(int i = 0; i < Sequence.length() - 1; i++)
Set.add((Sequence.substring(i, i + 2)));
BitSet buffer = Set.getBitSet();
Output.writeObject(buffer);
}
Input.close();
Output.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
I think you need a Scanner see this code Java Bitset error with large index. It's a different question but the first loop is to read a large file with numbers into a bitset,

Categories

Resources