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 3 years ago.
Improve this question
i want to display the first 4 characters of my dummy data.
How do i do that?
I want as output :
outputvalue = $*
outputvalue = 22
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
byte[] outData = ("$*220000000000101SDFRGGHYB0").getBytes();
ByteBuffer b = ByteBuffer.wrap(outData);
System.out.println("The structure"+ b);
outputvalue1 = $*
outputvalue2 = 22
}
byte[] outData = "$*220000000000101SDFRGGHYB0".getBytes("UTF-8");
ByteBuffer b = ByteBuffer.wrap(outData);
byte[] outputValue1 = new byte[2];
b.get(outputValue1);
byte[] outputValue2 = new byte[2];
b.get(outputValue2);
String outputString1 = new String(outputValue1, "UTF-8");
String outputString2 = new String(outputValue2, "UTF-8");
int outputNum2 = Integer.parseInt(outputString2);
String is always in Unicode, and binary data (byte[]) as text have some encoding, charset. This must be mentioned when converting between String/Reader/Writer and byte[]/InputStream/OutputStream
ByteBuffer allows positioned, random access reading and writing. The above uses linear reading.
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 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 last year.
Improve this question
I've got a text file with this format:
image (Excluding the headers)
I need to take the account number and pin as input from the user and then match it against the values in the text. That is, check if the pin is correct for the given account number. How would I do that?
You can parse the text file and fetch data as records using CSVParser and then you can match it with the user input.
Here is the sample code:
File dataFile = new File("dataFile.txt");
//delimiter is the character by which the data is separated in the file.
//In this case it is a '\t' tab space
char dataDelimiter = '\t';
try(InputStream is = new FileInputStream(dataFile);
InputStreamReader isr = new InputStreamReader(is);
//Initializing the CSVParser instance by providing delimiter and other configurations.
CSVParser csvParser = new CSVParser(isr, CSVFormat.DEFAULT.withDelimiter('\t').withFirstRecordAsHeader())
)
{
for (CSVRecord csvRecord : csvParser)
{
long accNumber = Long.parseLong(csvRecord.get("A/C No."));
long pinNumber = Integer.parseInt(csvRecord.get("Pin"));
//-----------------
}
}
catch (IOException e)
{
e.printStackTrace();
}
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.