I have searched the internet to search a chosen directory and use the contained .txt files to convert to .csv to work with the data in a spreadsheet. This program works tried and true with everything .txt and I figured I would share with you all my findings. The only thing left to put in is to limit only .txt files containing LOG in the first letters and it is a complete project for me.
The code is as follows:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class LogReader
{
/**
*
*/
public static void main(String[] args)
throws IOException
{
// Creates a GUI based chooser for the user to select the directory to
// evaluate.
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle("Folder Chooser");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
int returnVal = chooser.showOpenDialog(chooser);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
JOptionPane.showMessageDialog(null, "The current directory is:\n"
+ chooser.getSelectedFile().getAbsolutePath());
String sourceFolder = chooser.getSelectedFile().getAbsolutePath();
File folder = new File(sourceFolder);
String temp1;
String temp2 = "\\";
String temp3;
FileWriter output = new FileWriter("Output Log.csv");
BufferedReader reader;
// create list of files within a certain folder.
String[] files = folder.list();
for(int i = 0; i < files.length; i++)
{
if(files[i].lastIndexOf('.') > 0)
{
int lastIndex = files[i].lastIndexOf('.');
String str = files[i].substring(lastIndex);
if(str.equals(".txt"))
{
// Variables to concatenate the complete file directory
// pointer.
temp3 = files[i];
temp1 = sourceFolder.concat(temp2);
temp1 = temp1.concat(temp3);
// Use the BufferedReader with the new file pointer and
// store it
// in line.
reader = new BufferedReader(new FileReader(temp1));
String line = reader.readLine();
while(line != null)
{
StringTokenizer tokens = new StringTokenizer(line,
" ");
while(tokens.hasMoreTokens())
{
String word = tokens.nextToken();
output.append(word + ',');
}
output.append('\n');
// read next line
line = reader.readLine();
}
output.append('\n');
}
}
}
output.flush();
output.close();
// If the user selected cancel, then let them know.
}
else
{
JOptionPane.showMessageDialog(null, "No selection");
}
}
}
I just needed to add a Boolean to this to compare the string with certain requirements.
Related
Below java code is to split a big .csv file into multiple .csv files. But how to store Header in all splitted files?
import java.io.*;
import java.util.Scanner;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class split {
public static void main(String args[]) {
try {
String inputfile = "E:/Sumit/csv-splitting-2/Proposal_Details__c.csv";
System.out.println("Input Path is :- " + inputfile);
double nol = 100000.0;
File file = new File(inputfile);
Scanner scanner = new Scanner(file);
int count = 0;
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
System.out.println("Lines in the file: " + count);
double temp = (count / nol);
int temp1 = (int) temp;
int nof = 0;
if (temp1 == temp) {
nof = temp1;
} else {
nof = temp1 + 1;
}
System.out.println("No. of files to be generated :" + nof);
FileInputStream fstream = new FileInputStream(inputfile);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
for (int j = 1; j <= nof; j++) {
String outputpath = "E:/Sumit/csv-splitting-2/";
String outputfile = "File-2-Proposal_Details__c" + j + ".csv";
System.out.println(outputpath + outputfile);
FileWriter fstream1 = new FileWriter(outputpath + outputfile);
BufferedWriter out = new BufferedWriter(fstream1);
for (int i = 1; i <= nol; i++) {
strLine = br.readLine();
if (strLine != null) {
out.write(strLine);
if (i != nol) {
out.newLine();
}
}
}
out.close();
}
in.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Assuming your first line is the header, you can have a String header; that will get the read of the first line, eg: header = br.readLine();.
On your for loop for nof (which I assume means number_of_files), you always add the header as the first line when you create a new file.
It would be something like this:
before your for-loop, you just save the header on a variable
String header = br.readLine();
you have 2 for-loops, one that creates a file, the other one that write each line to the newly created file
Inside the first for loop, right after you create the file, you just write the header to it: our.write(header);
General tips:
use variable names that makes sense. nol, nof, j... none of them make sense, you can pretty much call them numOfLines, numOfFiles and currentFile for example.
So I am trying to use JFile chooser to read strings in from a .txt file and save them into an array. I though I had this a set up right but the problem is nothing is getting put into the array because when I display the array it is all nulls. I am new to java so I am not that familiar with using JFile chooser. The window pops up and everything and lets me choose a file so I think it is working somewhat. Also if you could explain to me what all of the code mean regarding the JFile chooser I would be very grateful, most of what I have is from videos, online and books and I kind of understand it but I definitely need some further explanation.
import java.io.*;
import java.util.Scanner; // All My imports
import javax.swing.*;
public class SpellChecker // I am creating a spell checker
{
static String check[] = new String[50]; // My array to save all of the strings in a file
static int index = 0; // index will incremented whenever a string is added to the array
public static void main(String args[])
{
getFile(); // Method call that opens file and saves all the data to the array
for(int i = 0; i < check.length - 1; i++) // outputting the array
{
System.out.println(check[i]);
}
}
public static void getFile()
{
JFileChooser chooser;
File infile, directory;
int status;
String wordToCheck;
chooser = new JFileChooser( );
status = chooser.showOpenDialog(null);
if(status == JFileChooser.APPROVE_OPTION)
{
infile = chooser.getSelectedFile();
directory = chooser.getCurrentDirectory();
try
{
infile = new File(chooser.getSelectedFile().getAbsolutePath());
Scanner scanner = new Scanner(infile);
while(scanner.hasNext())
{
wordToCheck = scanner.next();
if(index == check.length)
{
String tempAr[] = new String[check.length*2];
for(int i = 0; i < index; i++)
{
tempAr[i] = check[i];
}
check = tempAr;
}
check[index] = wordToCheck;
index++;
}
scanner.close();
}
catch(IOException e)
{
System.out.println("Error");
}
}
else
{
System.out.println("Open File dialog canceled");
}
}
I'm working on a project that takes in criteria supplied by a user, and compares it to an already created list of object containing similar criteria.
Currently, I'm trying to get the program to read the file, but I keep getting my exception and not what I want. My code for the scanner and file is as followed:
package project205;
import java.util.*;
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class HouseList {
ArrayList<House> houseList = new ArrayList<House>();
public HouseList(String fileName)
{
//Open the data file
Scanner myFileIn = null;
try
{
myFileIn = new Scanner(new File("houses.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("File: " + "houses.txt" + " is not found");
}
// First piece of data is the number of records
int numRecords = myFileIn.nextInt();
String address1;
int price1;
int area1;
int numBedroom1;
// Temp variable to accumulate the sum
//double sum = 0.0;
//Read the data line by line and build the
//array lists containing names and incomes
for (int k = 0; k < numRecords; k++)
{
address1 = myFileIn.next();
price1 = myFileIn.nextInt();
area1 = myFileIn.nextInt();
numBedroom1 = myFileIn.nextInt();
House house1 = new House(address1, price1, area1, numBedroom1);
houseList.add(house1);
}
// Close the input file
myFileIn.close();
}
public String getHouses(Criteria c)
{
String result = "";
for(int i = 0; i < houseList.size(); i++)
{
House h1 = houseList.get(i);
if (h1.satisfies(c))
{
result = result + h1.toString();
}
}
return result;
}
public void printHouses(Criteria c)
{
System.out.println(getHouses(c));
}
}
My file is in the same package, as I am using eclipse, but I keep getting "File: houses.txt is not found". To be thourough, the error I get is :
File: houses.txt is not found
Exception in thread "main" java.lang.NullPointerException
at project205.HouseList.<init>(HouseList.java:29)
at project205.HouseListTester.main(HouseListTester.java:7)
If anyone could even point me in the direction of what I'm missing here I would greatly appreciate it!
This may help you:
//creating File instance to reference text file in Java
File text = new File("<file location>/houses.txt");
//Creating Scanner instnace to read File in Java
Scanner scnr = new Scanner(text);
//Reading each line of file using Scanner class
while(scnr.hasNextLine()){
//process file
}
Use
System.getProperty("user.dir") + System.getProperty("file.separator") + "houses.txt"
to get the file path if houses.txt and the class file from which you are trying to access shares the same directory.
you can modify
myFileIn = new Scanner(new File("houses.txt"));
//to
myFileIn = new Scanner(new File(System.getProperty("user.dir") + System.getProperty("file.separator") + "houses.txt"));
//or
myFileIn = new Scanner(new File(System.getProperty("file.separator") + "houses.txt"));
Other case is that the file is in some other directory. In this scenario, provide relative path of this file ex.
//current dir is c: and your file is in d: then do
myFileIn = new Scanner(new File("addRelativeFilePathHere" + "houses.txt"));
Above, you need to end addRelativeFilePathHere with file.separator or prefix houses.txt with file separator.
This link to see what these properties points to and their meanings and for more inormation
http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html and
http://docs.oracle.com/javase/7/docs/api/java/io/File.html
I have a array list the contains data from a text file.
The text file is structured like this
1,2,Name,2,itemName
My code:
String Cid = "1";
String Tid = "1";
//Help
File iFile = new File("Records");
BufferedReader yourfile = new BufferedReader(new FileReader(iFile));
FileWriter writer = new FileWriter(iFile);
String dataRow = yourfile.readLine();
while (dataRow != null){
String[] dataArray = dataRow.split(",");
if(Cid.equals(dataArray[1]) && Tid.equals(dataArray[3]))
dataRow = yourfile.readLine();
else{
System.out.print(dataRow);
writer.append(dataArray[0]+", ");
writer.append(dataArray[1]+", ");
writer.append(dataArray[2]+", ");
writer.append(dataArray[3]+", ");
writer.append(dataArray[4]);
writer.append(System.getProperty("line.separator"));
dataRow = yourfile.readLine();
}
}
writer.flush();
writer.close();
}
I want to be able to remove the record where the Name id and Item id match.
everything I have read about removing items from array lists only talks about removing by item position. Any help would be much appreciated.
String Cid = "1";
String Tid = "1";
File iFile = new File("Records");
BufferedReader yourfile = new BufferedReader(new FileReader(iFile));
BufferedReader yourfile2 = new BufferedReader(new FileReader(iFile));
int total=0;
String rec=yourfile2.readLine();
while (rec != null){ // count total records (rows)
total++;
rec=yourfile2.readLine();
}
String dataRow = yourfile.readLine();
String[] allTemp[]=new String[total][]; //create array of an array with size of the total record/row
int counter=0;
while (dataRow != null){
String[] dataArray = dataRow.split(",");
if(Cid.equals(dataArray[1]) && Tid.equals(dataArray[3]))
dataRow = yourfile.readLine(); // skip current row if match found
else{
allTemp[counter]=dataArray; //if match not found, store the array into another array
dataRow = yourfile.readLine();
counter++; //index for allTemp array. note that counter start from zero and no increment if the current row is skipped
}
}
FileWriter writer = new FileWriter(iFile); //create new file which will replace the records file. here, all desired records from file already stored in allTemp array
for (String[] arr : allTemp){
//check nullity of array inside the array(record).
if(arr!=null){
for(int i=0;i<arr.length;i++){
writer.append(arr[i]);
if(i<arr.length-1) //add "," in every column except in the last column
writer.append(",");
}
writer.append(System.getProperty("line.separator"));
}
}
writer.flush();
writer.close();
Update:you can delete String[] temp; and temp = new String[dataArray.length]; since it was never used actually
I think you need to iterate over each element in your array list and for each String make a java.util.StringTokenizer with "," as the delimiter (I'm assuming there are no commas in Name or itemName).
Then get the 2nd and 4th tokens and compare them. If then match then remove that item.
It's probably most efficient if you use a for loop that starts at the end fo the ArrayList and moves to the 0th element, removing items by index as you find them.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Neat {
public static void main(String... string) throws FileNotFoundException {
File file = new File("c:/AnyFile.txt");
Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()) {
String text = fileScanner.nextLine();
String[] data = text.split(",");
int recordId = Integer.parseInt(data[0]);
int nameId = Integer.parseInt(data[1]);
String name = data[2];
int itemId = Integer.parseInt(data[3]);
String itemName = data[4];
if (nameId == itemId) {
removeLineFromFile(file, text);
}
}
}
public static void removeLineFromFile(File file, String lineToRemove) {
try {
File inFile = file;
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
// Construct the new file that will later be renamed to the original
// filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
// Read from the original file and write to the new
// unless content matches data to be removed.
while ((line = br.readLine()) != null) {
if (!line.trim().equals(lineToRemove)) {
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
// Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
// Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile))
System.out.println("Could not rename file");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Just get the stuff you want
I am trying to split text files in a directory along a line 'END OF CUSTOMER STATEMENT' and I store the result files into a temporary directory. The split happens only for the first file while the other file is ignored, what is the problem with my code. I was expecting the for loop will engulf all the files in the directory? Here is my code.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
*
* #author Administrator
*/
public class SplitFiles {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
File f = new File("D:/statements/");
String[] filenames = f.list();
File[] texts = f.listFiles();
String lines = "";
for (int m = 0; m < filenames.length; m++) {
try {
int count = 0;
FileInputStream fs = new FileInputStream("D:/statements/" + filenames[m]);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
FileOutputStream fos = new FileOutputStream("D:/DFCU Statements/statement" + count + ".RPT");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
while ((lines = br.readLine()) != null) {
String mine = lines.trim();
if (mine.startsWith("END OF CUSTOMER STATEMENT")) {
bw.close();
count++;
fos = new FileOutputStream("D:/DFCU Statements/statement" + count + ".RPT");
bw = new BufferedWriter(new OutputStreamWriter(fos));
continue;
}
if (mine.isEmpty()) {
continue;
} else {
bw.write(lines);
bw.newLine();
bw.flush();
}
}
fos.close();
fs.close();
br.close();
bw.close();
} catch (Exception ag) {
System.out.println(ag);
}
}
}
}
I think you should do this in the first place (there are possibly more bugs)
int count = 0;
for (int m = 0; m < filenames.length; m++) {
...
UPDATE besides, remove your count++ and place it after each file creation
FileOutputStream fos = new FileOutputStream("D:/DFCU Statements/statement" + count + ".RPT");
count++;
then it will work as expected
I assume that since the target files have nothing that distinguishes them from each other (they are all named statementX.RPT) - that the last file is actually the one you have in your output - but this is only a guess.
try to change your output file to be named "statement." + m + "." + count ".RPT" and that way you will have unique output files.
Also, take note to the following comments:
When using the File class, the listFiles API is more usefull (in my opinion) - from each file you get you can query getName and getPath.
About this line: FileInputStream fs = new FileInputStream("D:/statements/" + filenames[m]); - if you used the results you got from listFiles you could replace it with FileInputStream fs = new FileInputStream(files[m]); - no need to hard-code the path.
You should modify your code. Otherwise instead of creating two output files your code will create three output files. Here is the correct code
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
*
* #author Administrator
*/
public class SplitFiles {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
File f = new File("D:/statements/");
String[] filenames = f.list();
File[] texts = f.listFiles();
String lines = "";
int count = 0;
for (int m = 0; m < filenames.length; m++) {
try {
FileInputStream fs = new FileInputStream("D:/statements/" + filenames[m]);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
FileOutputStream fos = null;
BufferedWriter bw = null;
while ((lines = br.readLine()) != null) {
String mine = lines.trim();
if (mine.startsWith("END OF CUSTOMER STATEMENT")) {
if(bw!=null)
{
bw.close();
}
count++;
continue;
}
if (mine.isEmpty()) {
continue;
} else {
if(bw==null)
{
fos = new FileOutputStream("D:/DFCU Statements/statement" + count + ".RPT");
bw = new BufferedWriter(new OutputStreamWriter(fos));
}
bw.write(lines);
bw.newLine();
bw.flush();
}
}
fos.close();
fs.close();
br.close();
bw.close();
} catch (Exception ag) {
System.out.println(ag);
}
}
}
}