Reading all text files(data set) from one folder in java - java

Respected Members,
The topic has been discussed previously but the I have tried those. I am facing an issue in reading all text files from one folder. I am calculating the probability for each text file. Each text file has round about 1500 lines.The code I have shown is reading files from folder but it does not execute method for it.I have used two loops in code chunk. I tried to run execution with a value in "i " variable" in both loops. The while loops is executed before FOR loop(showing wrong logic) . I want it to execute "get.probability()" method for each text file. Kindly please look for the issue. It is only running the first file from folder named "cs.txt",calculates it's probability and detects its language
String target_dir = "./testdataset";
int i = 0;
BufferedReader inputStream = null;
File dir = new File(target_dir);
File[] files = dir.listFiles();
for (File f : files) {
if(f.isFile()) {
System.out.println("File name in directory is: " + f);
inputStream = new BufferedReader(new FileReader(f));
//System.out.println("i in FOR loop" + " " + i);
}
String line;
try {
while((line = inputStream.readLine()) != null) {
//System.out.println("i in while loop" + " " + i); just for checking
detector.append(inputStream);
}
//i++;
String lang = detector.detect();
ArrayList<Language> langlist = detector.getProbabilities();
System.out.println("Language Detected for input file is" + " " + lang);
System.out.println("Probability of language is: " + " " + langlist);
inputStream.close();
}
catch(Exception e) {}
}

I think your problem might due to the execution of the try block even though f might be a directory. You can use the continue (see this) keyword to skip to the next iteration of the loop if f is not a file.
I know nothing about the detector, but make sure that input is cleared after inputStream.close() is called, otherwise you might append multiple files to a single detector.
for (File f : files) {
//This will skip the file if it is a directory
if (!f.isFile())
continue;
System.out.println("File name in directory is: " + f);
inputStream = new BufferedReader(new FileReader(f));
String line;
try {
while((line = inputStream.readLine()) != null) {
//System.out.println("i in while loop" + " " + i); just for checking
detector.append(inputStream);
}
//i++;
String lang = detector.detect();
ArrayList<Language> langlist = detector.getProbabilities();
System.out.println("Language Detected for input file is" + " " + lang);
System.out.println("Probability of language is: " + " " + langlist);
inputStream.close();
}
catch(Exception e) {}
}

for reading file just use :
import org.testng.reporters.Files;
String data =Files.readFile(file);
the code will be cleaner and you can do what ever you want

Related

Reading a .txt file using BufferedReader and FileReader

I used BufferedReader and FileReader to read a file but everytime I read it, it just displays no name found. Thanks in advance.
BufferedReader ifile = new BufferedReader (new FileReader ("data.txt"));
String N;
while(true)
{
N=ifile.readLine();
if (N == null){
System.out.print("\fNo name found\n");
break;
}
number = Integer.parseInt(ifile.readLine());
house = ifile.readLine();
form = ifile.readLine();
dob = ifile.readLine();
System.out.println("Name: " + N + "\nNumber: " + number + "\nHouse: " + house + "\nForm: " + form + "\nDate of Birth: " + dob);
}
ifile.close();
Answer might have already been given in another topic like: Read all lines with BufferedReader
So it might be a duplicate.
However when you read a file via BufferedReader it is recommended you do it like
FileReader filereader = new FileReader("data.txt");
BufferedReader ifile = new BufferedReader(filereader);
String N;
ArrayList<String> file_contents= new ArrayList<String>();
//List will now contain the whole txt
try {
while((N = input.readLine()) != null) {
file_contents.add(N);
}
ifile.close();
}
catch(IOException e){
e.printStackTrace();
}
And then break the list's contents to get what you want.
Using a try/catch block can avoid the error of not knowing how to handle that the file "data.txt" cannot be read.
Your way of doing it (while(true)) doesn't pass the name in any variable so it can be printed out and also just checks if the 1st line of your data.txt file is empty or not and does nothing with the remaining lines if that condition is true.
In addition to the above check if the source of your problem is in the txt file.
For example if its structure is the way you want it to be.

Java code for read .CSV file and FTP server but data writing in single line

I have case Java code for read .CSV file which contains multiple lines data with headers and write into FTP server but data writing in single line.
Can any one guide how to get proper output same like input file.
Code:
finally{
try {
// Cookcounty FTP start
if (EnrollmentEDI.Status != "FAILURE") {
if (CountyPlanNumbersList.contains(samp.PlanNumber())) {
CommonMessages.logGenericTrace("CountyFTp start");
com.vitria.connectors.ftp.DynamicFTPTargetInfo CountyFTP = com.vitria.connectors.ftp.FTPConnectorUserLib.createDynamicFTPTargetInfo();
CountyFTP.setHostname(props.getProperty("County_HostName"));
CountyFTP.setUsername(props.getProperty("County_UserName"));
CountyFTP.setPassword(props.getProperty("County_Password"));
CountyFTP.setDirectory(props.getProperty("County_RemoteDirectory"));
FileInputStream fins = null;
BufferedInputStream bins = null;
DataInputStream dins = null;
String line1 = "";
File CookCoutyOut1 = new File(utility.CountyOutputFolderPath + "/" + ccformattedDate + ".csv");
CommonMessages.logGenericTrace("CountyFileName=" + CookCoutyOut1);
fins = new FileInputStream(CookCoutyOut1);
bins = new BufferedInputStream(fins);
dins = new DataInputStream(bins);
while (dins.available() != 0) {
line1 = line1 + dins.readLine();
;
}
if (!(line1.trim().equals(""))) {
CookCountyOriginal = line1;
}
dins.close();
bins.close();
fins.close();
// CommonMessages.logGenericTrace("CookCountyOriginal =" + CookCountyOriginal);
String cookyFileName = ccformattedDate + ".csv";
CommonMessages.logGenericTrace("cookyFileName =" + cookyFileName);
EventDef newEventFile2 = (EventDef) (vtFileConnectorEvents.dataFileEventInterfaceHelper.getMetaObject().findDef("dataFileEvent"));
Object[] outParamsFile2 = {CookCountyOriginal.getBytes(), cookyFileName};
EventBody out3 = null;
out3 = JctLib.createEventBody(newEventFile2, outParamsFile2);
getOutPort8(CountyFTP).push(out3);
}
CommonMessages.logGenericTrace("CountyFTp END");
}
} catch (Exception c) {
CommonMessages.logGenericTrace(" *** INFORCE ENROLLMENT PROCESS : ERR IN County :-" + c.toString());
FTPtrack = "" + c.toString();
FTPtrack = "FTP TRANSFER TO MAINFRAME :" + FTPtrack.replaceAll(":", ",") + " - FAILED";
EnrollmentEDI.Track = EnrollmentEDI.Track + " FTPEXCEPTION : " + FTPtrack + "\n";
//CommonMessages.logGenericTrace("Exception in ACK FTPtrack======="+FTPtrack);
EnrollmentEDI.Status = "FAILURE";
EnrollmentEDI.Intouchtrack = EnrollmentEDI.Intouchtrack + "\n" + "FTPEXCEPTION :" + c.toString() + "\n-RESOLUTION:" + props.getProperty("5");
com.glic.excp.handlers.GLICPCExcpHandler glicpc = new com.glic.excp.handlers.GLICPCExcpHandler();
glicpc.handleComponentException(c, ctx);
}
}
The readLine() method only reads up to the newline. The string returned does not include the newline character. See the contract of DataInput#readLine.
To add the newline back on, you could replace this loop:
while (dins.available() != 0) {
line1 = line1 + dins.readLine();;
}
with one that adds either "\n" or "\r\n" or System.getProperty("line.separator") to the end of each line read, depending on what kind of line endings the mainframe expects.
But there are a few other problems with this code. You should be checking for a null result from readLine() to detect end-of-file, not looking at dins.available(). And you should be using BufferedReader, not DataInputStream as the documentation suggests. DataInputStream#readLine has been deprecated since JDK 1.1.
The loop would look like this if you continue using available() and DataInputStream:
String lineSeparator = System.getProperty("line.separator");
while (dins.available() != 0) {
line1 = line1 + dins.readLine() + lineSeparator;
}
Or like this in a more up-to-date style of Java, using BufferedReader, a try-with-resources statement, and just using "\n" for a newline instead of line.separator:
try (BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(CookCoutyOut1)))) {
String line;
while ((line = br.readLine()) != null) {
line1 += line + "\n";
}
}
You should find out what kind of line endings the machine you're uploading this to expects. Using System.getProperty("line.separator") is going to give you the default line separator for the machine you're running on, but that's not necessarily the same as what the machine you're sending it to wants. In the last example I just wrote an explicit "\n". You could replace that with "\r\n" if the mainframe wants carriage return/linefeed (CRLF).

Can't replace string in a text file

Following this thread:
I have a text file storing usernames password and bestscore for that user.
Trying to make a simple quiz game. I have a signup panel and when user signs up I store the data in this file and make his best score 0 for the new user.
The text file format for every single line is
{username} {password} {bestScore}
And when the user makes more than his bestScore i try to replace the actual score in the text file with the bestScore.
Well, back to that thread. I did everything line #meriton posted but the text file still hadn't changed. Here is my code:
if (gameData.getBestScore() < gameData.getScore()) {
int oldBestScore = gameData.getBestScore();
String oldLine = gameData.getCurrentUser() + " " + gameData.getCurrentUserPassword() + " " + oldBestScore;
gameData.setBestScore(gameData.getScore());
String newLine = gameData.getCurrentUser() + " " + gameData.getCurrentUserPassword() + " " + gameData.getBestScore();
// TODO replace the points in the text file
//first method
/*try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("C:\\Users\\Niki\\Desktop\\Java Projects\\QuizGame\\QuizGame\\usernames.txt"))))) {
String line = br.readLine();
while (line != null) {
if (line.contains(gameData.getCurrentUser())) {
String newLine = gameData.getCurrentUser() + " " + gameData.getCurrentUserPassword() + " " + gameData.getBestScore();
line = line.replace(line, newLine);
break;
}
line = br.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
//second method
Path path = Paths.get("C:\\Users\\Niki\\Desktop\\Java Projects\\QuizGame\\QuizGame\\usernames.txt");
Charset charset = StandardCharsets.UTF_8;
String content = null;
try {
content = new String(Files.readAllBytes(path), charset);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(content);
content = content.replaceAll(oldLine, newLine);
System.out.println(content);
gameOverPanel.gameOverLabel.setText("<html><h1>You didn't answer correctly!</h1><hr><h2>The correct answer is: " + gameData.getCurrentQuestion().getCorrectAnswer().getText() + "</h2><h3>Congratulations! New High Score: " + gameData.getBestScore() + "</h3></html>");
}
else {
gameOverPanel.gameOverLabel.setText("<html><h1>You didn't answer correctly!</h1><hr><h2>The correct answer is: " + gameData.getCurrentQuestion().getCorrectAnswer().getText() + "</h2><h3>Your score: " + gameData.getBestScore() + "</h3></html>");
}
}
As you can see I println before editing the content and after that to the console and everything is ok there. The old content is replaced with the new one but the file is not updated with the new content.
Also i tried to do it my way you can see the commented section in my code under the //first method comment. That way still didn't work.
Here:
System.out.println(content);
content = content.replaceAll(oldLine, newLine);
System.out.println(content);
That updates your String variable in memory. That's all this does. But there is no "magic" connection between that value in your memory and the file on disc. That string variable does neither know nor care that you read its content from a file initially.
If you want to update the file content; then you have to write back the changed string into your file. See here for ideas how to do that.
Try to write the content of the variable into the source file.
Files.write(path, content.getBytes(), StandardOpenOption.CREATE);
You have just loaded the content of file in memory and applied the replaceAll on the content variable.
But you must save change into source file.

Java write to file and save it by fileChooser

I am new to java and moving my codes since last two years from VB to java using netbeans 8 .
Now i want to write loop acumalted data to file and last
to save produced file to specific place using FlieChooser below
is my code but i can't see any file in My Desktop when I wrote name in doilog and press enter :
public void SaveToFile() throws IOException {
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("test.txt"), "utf-8"))) {
int i=0;
String Data[]=new String[10];
while( i<10 ){
writer.write("Student No :" + i);
Data[i]= "Student No :" + i;
++i;
}
}
int userSelection = db.showSaveDialog(this);
if (userSelection == JFileChooser.APPROVE_OPTION) {
File fileToSave = db.getCurrentDirectory();
String path = fileToSave.getAbsolutePath();
path = path.replace("\\", File.separator);
System.out.println("Save as file: " + path);
}
}
I see a couple problems with this. One, none of the code you're displaying here shows a call to any ".Save()" or copy or move method after choosing the directory. Two, the File object is pointing at a directory, not a file name. Three, your initial Writer object is probably writing test.txt to the directory your .class or .jar file lives in while it's running.
You need to figure out the directory and file name you want to use BEFORE you start writing to the disk.
UPDATE
public void SaveToFile() throws IOException {
int userSelection = db.showSaveDialog(this);
if (userSelection == JFileChooser.APPROVE_OPTION) {
File fileToSave = db.getCurrentDirectory();
String path = fileToSave.getAbsolutePath() + File.separator + "test.txt";
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(path), "utf-8"))) {
int i=0;
//String Data[]=new String[10];
while( i<10 ){
writer.write("Student No :" + i);
//Data[i]= "Student No :" + i; // Not sure why Data[] exists?
++i;
}
}
System.out.println("Save as file: " + path);
}
}
I think this is approximate to what you will need. I don't have a java compiler at the moment, so I can't say for sure if that's all good syntax. But there are plenty of Java tutorials online.

How can I stop my bufferedWriter from re-writing the text which is already in the file?

I have a BufferedWriter which is being used to write to a file the users details. I have noticed, however, it is not writing to file in the format I want it to, I can't seem to find a method which allows me to write text on a new line without it writing over what is already there or copy out the text already in the file. Is there any way I can edit the following code, to allow it to the above?
Details = "Name: " + name + " CardNo: " + CardNo + " Current Balance: " + balance + " overdraft? " + OverDraft + " OverDraftLimit: " + OverDraftLimit + " pin: " + PinToWrite;
try{
//Create writer to write to files.
File file = new File(DirToWriteFile);
FileOutputStream fos = new FileOutputStream(file, true);
Writer bw = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader("VirtualATM.txt");
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
String currentData = "";
while((currentData=bufferedReader.readLine()) != null) {
line = currentData;
bw.write(currentData);
((BufferedWriter) bw).newLine();
}
bw.write(Details);
System.out.println("Account created!");
There is no problem with the program (all variables work properly and have been declared somewhere) I have only posted a snippet of the code which is relevant to the question. The current output to the file after running it a few times looks like this
I've got it! Change:
while((currentData=bufferedReader.readLine()) != null) {
line = currentData;
bw.write(currentData);
((BufferedWriter) bw).newLine();
}
bw.write(Details);
to:
bw.append(Details);
((BufferedWriter) bw).newLine();
This ensures that no data will be written twice and it will not copy out any text besides the information gained from the user.

Categories