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();
}
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 debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
While reading csv file data and storing large amount of records into mysql database by using JPA repository save() method. Not able to insert whole data only one record is updating in database. Suggest me where i am ding wrong.
private void readFileFromFile(FileUploadDetails fileUploadDetail) throws IOException {
String filePath = fileUploadDetail.getFilePath();
try {
FileReader filereader = new FileReader(filePath);
CSVReader csvReader = new CSVReaderBuilder(filereader).withSkipLines(1).build();
List<String[]> nextRecord;
List<FileAnnualEnterpriseSurveyLog> fileAnnualEnterpriseSurveyLog = new ArrayList<>();
FileAnnualEnterpriseSurveyLog fileAESurveyLogVo = new FileAnnualEnterpriseSurveyLog();
// we are going to read data line by line
List<String[]> allData = csvReader.readAll();
int count = 0;
for (String[] cell : allData) {
fileAESurveyLogVo.setYear(cell[0].toString());
fileAESurveyLogVo.setIndustryAggregationNZSIOC(cell[1].toString());
fileAESurveyLogVo.setIndustryCodeNZSIOC(cell[2].toString());
fileAESurveyLogVo.setIndustryNameNZSIOC(cell[3].toString());
fileAESurveyLogVo.setUnits(cell[4].toString());
fileAESurveyLogVo.setVariableCode(cell[5].toString());
fileAESurveyLogVo.setVariableName(cell[6].toString());
fileAESurveyLogVo.setVariableCategory(cell[7].toString());
fileAESurveyLogVo.setValue(cell[8].toString());
fileAESurveyLogVo.setIndustryCodeANZSIC06(cell[9].toString());
fileAnnualEnterpriseSurveyLog.add(fileAESurveyLogVo);
// fileAnnualEnterpriseSurveyRepository.save(fileAESurveyLogVo);
// fileAnnualEnterpriseSurveyRepository.
System.out.println(count);
count++;
}
System.out.println(fileAnnualEnterpriseSurveyLog.size()); // 277777
for (FileAnnualEnterpriseSurveyLog file : fileAnnualEnterpriseSurveyLog) {
fileAnnualEnterpriseSurveyRepository.save(file);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
After you have inserted the first object, you are updating always the same. You should create a new element at each iteration.
for (String[] cell : allData) {
fileAESurveyLogVo = new FileAnnualEnterpriseSurveyLog();
fileAESurveyLogVo.setYear(cell[0].toString());
fileAESurveyLogVo.setIndustryAggregationNZSIOC(cell[1].toString());
... etc ...
}
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I'm creating a sport prediction game for my Grade 11 year and I'm having issues writing data to a text file. I'm using NetBeans 7.3.1. I'm using a button where every time it is pressed data entered by the user must be written to the text file. The text file is empty in the beginning and I need to add data to it. After the first click on the button the data keep rewriting itself and the new data is not added. It needs to be in a new line each time. Thank you very much. Some coding would be awesome!
I just did a quick search for appending to a file (usually a good thing to do): this question seems to be what your looking for.
I haven't tested this, but this should work:
private boolean appendToFile(String fileName, String data, String lineSeparator)
throws IOException {
FileWriter writer = null;
File file = new File(fileName)
if (!file.exists()) {
file.createNewFile();
}
try {
writer = new FileWriter(fileName, true);
writer.append(data);
writer.append(lineSeparator);
} catch (IOException ioe) {
return false;
} finally {
if (writer != null) {
writer.close();
}
}
return true;
}