Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to serialize a JTree object to a file.
This is how I am doing it:
FileOutputStream f = new FileOutputStream(new File("serialisation2.txt"));
ObjectOutputStream o = new ObjectOutputStream(f);
// Write objects to file
o.writeObject(jTree1);
o.close();
f.close();
Then I am trying to read it with this:
FileInputStream fi = new FileInputStream(new File("serialisation2.txt"));
ObjectInputStream oi = new ObjectInputStream(fi);
try{
while(true){
jTree2 = new javax.swing.JTree(raíz);
System.out.println(oi.toString());
jTree2 = (JTree) oi.readObject();
}
}
But the thing is, it returns java.io.EOFException when I do oi.readObject();
Any ideas?
remove while loop, you do not really need jTree2 = new javax.swing.JTree(raíz); just jtree2 can get the deserialized value. read more about serialization here and here
FileInputStream fi = new FileInputStream(new File("serialisation2.txt"));
ObjectInputStream oi = new ObjectInputStream(fi);
jTree2 = (JTree) oi.readObject();
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
I need to extract encryptedKey, salt, derivationMethod, derivationIterations parameters from bitcoin-like wallets (bitcoin/litecoin/dogecoin) like it is done here, but in java: https://github.com/openwall/john/blob/bleeding-jumbo/run/bitcoin2john.py
As I understand it, you need to read blocks of bytes of size 16384 until one of them contains the string mkey, but I do not yet understand how to do it all.
I did the following, but I don't know how to read the parameters further or if I even did the right thing:
FileInputStream walletFile = new FileInputStream(datFile);
byte[] curBlock = new byte[16384];
int bytesRead = walletFile.read(curBlock);
while (bytesRead > 0) {
String blockAsString = new String(curBlock, StandardCharsets.US_ASCII);
if (blockAsString.toLowerCase().contains("mkey")) {
//reading parameters
}
curBlock = new byte[16384];
bytesRead = walletFile.read(curBlock);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to split a txt file into multiple ones having the same size. I managed to do that using this function :
public static int fileSplitting(String fichier, String dossSortie, int nbMachines) throws FileNotFoundException, IOException{
int i=1;
File f = new File(fichier);
//FileReader fr = new FileReader(f);
//BufferedReader br = new BufferedReader(fr);
int sizeOfFiles = (int) (f.length()/(nbMachines));
System.out.print(sizeOfFiles);
byte[] buffer = new byte[sizeOfFiles];
try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(f))){
int tmp = 0;
while ((tmp = bis.read(buffer)) > 0) {
//write each chunk of data into separate file with different number in name
File newFile = new File(dossSortie+"S"+i);
try (FileOutputStream out = new FileOutputStream(newFile)) {
out.write(buffer, 0, tmp);//tmp is chunk size
}
i++;
}
}
return i;
}
The thing is that this function cut the words, while I need to keep every word.
For example, if I have a txt file "I live in Amsterdam", the function will split it like that: "I live in Ams", "terdam". I would like something like: "I live in", "Amsterdam".
I couldn't do the job, but I split my file into an array of words and then divided my file into files with equal number of words. It's not exactly what I wanted to do and it's not a "beautiful way" to do it, but it's not that bad.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How do I get an XML file from the assets folder using a function and save the result of the function to a String variable?
private String getXmlFile(String pathFile){
String xmlFileString = null;
AssetManager am = context.getAssets();
try {
InputStream str = am.open(pathFile);
int length = str.available();
byte[] data = new byte[length];
//str.what you want somthing to do...eg read and write str.read(data); and st.write(data);
xmlFileString = new String(data);
} catch (IOException e1) {
e1.printStackTrace();
}
return xmlFileString;
}
Maybe you can consider this AssetManager(http://developer.android.com/reference/android/content/res/AssetManager.html)
Help Full For u
AssetManager assetManager = getBaseContext().getAssets();
InputStream is = assetManager.open("order.xml");
Xml Parsing From Asset in Android
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to read huge pdf file using byte Array,
Here is my code for it.
String RESULT "D/new/certficateVisualFinal.pdf";
try {
FileInputStream fileInputStream=null;
File file = new File(RESULT);
byte[] bFile = new byte[(int) file.length()];
//convert file into array of bytes
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
reader = new PdfReader(bFile);
pdfStamper = new PdfStamper(reader, new FileOutputStream(outPut));
pdfStamper.setOutlines(outlineVisual);
pdfStamper.setFormFlattening(true);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
But I got a OutOfMemoryErro when trying to
fileInputStream.read(bFile);
This is the Error
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:220)
please help me.Thank you.
Don't use the byte array at all. PdfReader has a constructor with an InputStream parameter, so you can just pass your FileInputStream directly to that.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a file dictionary.txt and I need to get that file from the raw folder so I googled around and found this:
Here's an example:
"android.resource:///"+getPackageName()+ "/" + R.raw.dictionary;
but it did not work, Any idea?
Edit:
here is what i am doing
for(String line: FileUtils.readLines(file))
{
if(line.toLowerCase().equals(b.toLowerCase()))
{
valid = true;
}
}
You can get an input stream on a raw resource by using:
getResources().openRawResource(R.raw.dictionary);
EDIT:
From your edit, there really isn't a reason as to why you would specifically need the file rather than just using the input stream available from the openRawResource(int id) method...just use an existing java class http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#Scanner%28java.io.InputStream,%20java.lang.String%29.
Scanner scanner = new Scanner(getResources().openRawResource(R.raw.dictionary));
while(scanner.hasNextLine()){
{
if(scanner.nextLine().toLowerCase().equals(b.toLowerCase()))
{
valid = true;
}
}
}
Unfortunately you can not create a File object directly from the raw folder. You need to copy it in your sdcard or inside the application`s cache.
you can retrieve the InputStream for your file this way
InputStream in = getResources().openRawResource(R.raw.yourfile);
try {
int count = 0;
byte[] bytes = new byte[32768];
StringBuilder builder = new StringBuilder();
while ( (count = is.read(bytes,0 32768) > 0) {
builder.append(new String(bytes, 0, count);
}
is.close();
reqEntity.addPart(new FormBodyPart("file", new StringBody(builder.toString())));
} catch (IOException e) {
e.printStackTrace();
}
EDIT:
to copy it over to internal storage:
File file = new File(this.getFilesDir() + File.separator + "fileName.ext");
try {
InputStream inputStream = resources.openRawResource(R.id._your_id);
FileOutputStream fileOutputStream = new FileOutputStream(file);
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0) {
fileOutputStream.write(buf,0,len);
}
fileOutputStream.close();
inputStream.close();
} catch (IOException e1) {}
Now you have a File that you can access anywhere you need it.
Place the file in the assets folder and open it like this
getResources().getAssets().open("dictionary.txt");