Using a setter to change a file name - java

I need to create a setter, so that I can change the name of the merged file as I please. The exportToSingleFile() method takes the arrray input and creates a .csv file for each student and the mergeFile() method reads all of the files and merges the information into 1 output file. I need to rename this output file.
My main method is as follows:
import java.lang.*;
import java.io.*;
public class HW2_Main {
public static void main(String[] args) throws IOException {
myFile studentFiles = new myFile();
studentFiles.exportToSingleFile();
studentFiles.mergeFile();
studentFiles.setFileName("Can't Figure Out");
}
}
This is the new class myFile that I am getting all of my information from to create the objects:
class myFile{
// INPUTS
String[] headers = {"COURSE ID", "TEAM ID", "STUDENT FIRST NAME", "STUDENT LAST NAME",
"STUDENT ID", "ASSIGNMENT ID", "DATE SUBMITTED", "TIME SUBMITTED", "SUBMITTED BY"};
String[][] values = {
{"CMPS280-01", "PELICAN01", "FIRST-1", "LAST-1",
"W1234567", "H01", "08/22/17", "23:53", "W1234567"},
{"CMPS280-01", "PELICAN01", "FIRST-2", "LAST-2",
"W1234566", "H01", "08/22/17", "23:54", "W1234566"},
{"CMPS280-01", "PELICAN01", "FIRST-3", "LAST-3",
"W1234568", "H01", "08/22/17", "20:15", "W1234568"}};
myFile(){
}
void exportToSingleFile() throws IOException{
/*WRITE STUDENT INFO TO TEXT FILES*/
for (int i = 0; i < values.length; i++) {
//Generate file name for each student and prepare for writing to file
PrintWriter printToFile = new PrintWriter(new File("INPUT/"+values[i][1]+"_"+values[i][2]+"_"+values[i][3]+".csv"));
//Write headers to the file
for (int j = 0; j < headers.length - 1; j++) {
printToFile.print(headers[j] + ",");
}
printToFile.println(headers[headers.length - 1]);
//Write values to the file
for (int j = 0; j < headers.length - 1; j++) {
printToFile.print(values[i][j] + ",");
}
printToFile.print(values[i][headers.length - 1]);
printToFile.close();
}//end for
}
void mergeFile() throws IOException{
PrintWriter printToFileAllSubmit = new PrintWriter(new File("OUTPUT/All_Submit.csv"));
//Write headers to the file All_Submit_
for (int j = 0; j < headers.length - 1; j++) {
printToFileAllSubmit.print(headers[j] + ",");
}
printToFileAllSubmit.println(headers[headers.length - 1]);
//Get list of files in folder SUBMIT
File[] fileList = new File("INPUT").listFiles();
for (File f : new File("INPUT").listFiles()){
//System.out.println(f.getName());
BufferedReader bufRead = new BufferedReader(new FileReader(f));
//Read successive lines
while (bufRead.readLine() != null) {
printToFileAllSubmit.println(bufRead.readLine());
}
bufRead.close();
}
printToFileAllSubmit.close();
}
void setFileName(String newFileName){
}
}

Related

Is there a way to have a directory run through the main program?

I am having difficulty passing a directory to the code below. When I am prompted to enter a directory, I do as shown below: C:\Users. I get a 0 byte output, which is not accurate. Meaning that the program is not registering the typed directory.
code:
//import jdk.internal.icu.text.UnicodeSet;
import java.io.File;
import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
public class Hwk2018
{
public static void main(String[] args)
{
//String s = "C:\\Users\\
//File filess = new File(s);
System.out.println("Enter a directory or a file: ");
Scanner input = new Scanner(System.in);
String directory = input.nextLine();
Hwk2018 obj = new Hwk2018();
System.out.println(obj.getSize(new File(directory)) + " bytes");
}
int i = 0;
Queue<File> que = new LinkedList<>();
public long getSSize(File directory)
{
long size = 0;
que.add(directory);
while(!que.isEmpty())
{
File t = que.poll();
if(!t.isDirectory())
{
size += t.length();
}
else
{
//for(int i = 0; )
que.add(directory);
}
}
return size;
}
public static long getSize(File file)
{
long size = 0;
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; files != null && i < files.length; i++) {
size += getSize(files[i]);
}
} else {
size += file.length();
}
return size;
}
}
output when running ' MBP src % java Hwk2018 ' on the terminal and subsequently typing C:\Users:
0 bytes
Expected Output:
87 bytes (or some numerical value other than 0)
I would use File#exists to verify that the user input is a valid path, you could also add File#isDirectory check, but since getSize is doing this, it's probably not required.
subsequently typing C:\Users:
I'm using a Mac
Mac's don't have a concept of "drives" like windows, they have "volumes" and as such it should probably be /Users, but you could run into other issues, since you won't have read access to other users.
The following will print the path of your "home" directory and also uses File#exist to verify the user input
import java.io.File;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//String s = "C:\\Users\\
//File filess = new File(s);
String directory = System.getProperty("user.home");
System.out.println("Your home directory is [" + directory + "]");
System.out.println("Enter a directory or a file: ");
Scanner input = new Scanner(System.in);
directory = input.nextLine();
File parent = new File(directory);
if (parent.exists()) {
Main obj = new Main();
System.out.println(obj.getSize(new File(directory)) + " bytes");
} else {
System.out.println(directory + " is not a valid directory");
}
}
public static long getSize(File file) {
long size = 0;
System.out.println("Scanning " + file.getName());
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; files != null && i < files.length; i++) {
size += getSize(files[i]);
}
} else {
size += file.length();
}
return size;
}
}

How can I take the length from the text files in java?

I am new in java. Firstly, I'm sorry for my English. I wrote a code to merge two different txt files in the mergedFile.txt with determined data from the data1.txt and data2.txt. I create 5 different arrays to use them better but I cannot learn the length of words in the textfiles so arrays are using determined parameter. If I want to add another student, this codes don't work. Can you help me?
data1.txt
ID,Name,LastName,Department
12345,John,Samon,Computer Science
14524,David,Souza,Electric and Electronic
.
.
data2.txt
ID,Q1,Q2,Q3,Midterm,Final
12345,100,90,75,89,100
14524,80,70,65,15,90
.
margedFile.txt
ID,Name,Q_Average,Midterm,Final,Department
12345,John,88.3,89,100,Computer Science
14524,David,67.0,100,70,Electric and Electronic
This is ReadData Class
import java.io.FileInputStream;//import java.io library
import java.util.Scanner;//import scanner library
public class ReadData {
public static String[] Read(String filename,String filename2) {
Scanner scan = null;
Scanner scan1 = null;/
FileInputStream input1 = null;
FileInputStream input = null;
String[] result = new String[3];
try {
input = new FileInputStream(filename);
scan = new Scanner(input);
input1 = new FileInputStream(filename2);
scan1 = new Scanner(input1);
String[][] myArray = new String[4][4];
String[][] myArray1 = new String[4][6];
while(scan.hasNext() || scan1.hasNext()) {
for (int i = 0; i < myArray.length; i++) {
String[] split =
scan.nextLine().trim().split(",");
for (int j = 0; j < split.length; j++)
{
myArray[i][j] = split[j];
}
}
for (int i = 0; i < myArray1.length; i++) {
String[] split1 = scan1.nextLine().trim().split(",");
for (int j = 0; j < split1.length; j++) {
myArray1[i][j] = split1[j];
}
}
}
int[][] quiz = new int[3][3];
double[] average = new double[3];
int sum = 0;
double averagee = 0;
for (int i = 0; i < quiz.length; i++) {
for (int j = 0; j < quiz.length; j++) {
quiz[i][j] = Integer.parseInt(myArray1[i+1][j+1]);
sum += quiz[i][j];
}
averagee = sum/quiz.length;
average[i] = averagee;
sum = 0;
}
for (int i = 1; i < myArray1.length; i++) {
for (int j = 1; j < myArray1.length; j++) {
if(myArray[i][0].equalsIgnoreCase(myArray1[j][0])) {
result[i-1] = "\n" + myArray[i][0] + " "+ myArray[i][1] + " " + (average[j-1]) +" "+ myArray1[j][4] +" " + myArray1[j][5] + " "+ myArray[i][3];
//System.out.println(Arrays.deepToString(myArray[i]) + " " + Arrays.deepToString(myArray1[j]));
}
}
}
//System.out.println(Arrays.deepToString(quiz));
//System.out.println(Arrays.toString(average));
}
catch(Exception e) {
System.out.println(e);
}
return result;
}
}
This is WriteData class
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
public class WriteData extends ReadData {
void Write(String filename) {
PrintWriter writer = null;
FileOutputStream output = null;
try {
output = new FileOutputStream(filename);
writer = new PrintWriter(output);
writer.print("ID,Name,Q_Average,Midterm,Final,Department ");
writer.print(Arrays.toString(Read("data1.txt",
"data2.txt")));
writer.flush();
writer.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}

Printing ListArrays to a txt file

I have a method that is suppose to take 3 arrays and save them to a txt file. But it doesnt print to the list. Here is my code for the method
public static void saveAndExit(int count, String[] makeArray, String[] moduleArray, double [] msrpArray) throws IOException{
File carList = new File("cars_list.txt");
PrintWriter pw = new PrintWriter(carList);
int index = 0;
String[] finalMSRPArray = new String[12];
for (int i = 0; i < msrpArray.length; i++){
finalMSRPArray[i] = String.valueOf(msrpArray[i]);
}
while(count < makeArray.length && count < moduleArray.length && count < msrpArray.length){
pw.write(makeArray[index]+"\n");
pw.write(moduleArray[index]+"\n");
pw.write(finalMSRPArray[index]+"\n");
index++;
}
pw.close();
}
Any help is much appreciated!

creating a model using naive bayes

Iam using lingpipe tool for naive bayes algorithm.I trained it using my trained data and it successfullu tests my test data. But each time I runs the algorithm each time it trains. I don't want to train it each time instead I want to build a model to which I can apply the test data.
public class ClassifyNews {
private static File TRAINING_DIR= new File("train");
private static File TESTING_DIR= new File("test");
private static String[] CATEGORIES
= { "c1",
"c2",
"c3"};
private static int NGRAM_SIZE = 6;
public static void main(String[] args)throws ClassNotFoundException, IOException
{
DynamicLMClassifier<NGramProcessLM> classifier
=DynamicLMClassifier.createNGramProcess(CATEGORIES,NGRAM_SIZE);
for(int i=0; i<CATEGORIES.length; ++i)
{
File classDir = new File(TRAINING_DIR,CATEGORIES[i]);
if (!classDir.isDirectory())
{
String msg = "Could not find training directory="+ classDir
+ "\nTraining directory not found";
System.out.println(msg);
throw new IllegalArgumentException(msg);
}
String[] trainingFiles = classDir.list();
for (int j = 0; j < trainingFiles.length; ++j)
{
File file = new File(classDir,trainingFiles[j]);
String text = Files.readFromFile(file,"ISO-8859-1");
System.out.println("Training on " + CATEGORIES[i] + "/" + trainingFiles[j]);
Classification classification= new Classification(CATEGORIES[i]);
Classified<CharSequence> classified= new Classified<CharSequence>(text,classification);
classifier.handle(classified);}
}
System.out.println("Compiling");
JointClassifier<CharSequence> compiledClassifier
= (JointClassifier<CharSequence>)
AbstractExternalizable.compile(classifier);
boolean storeCategories = true;
JointClassifierEvaluator<CharSequence> evaluator =
new JointClassifierEvaluator
<CharSequence> (compiledClassifier,CATEGORIES,storeCategories);
for(int i = 0; i < CATEGORIES.length; ++i)
{
File classDir = new File(TESTING_DIR,CATEGORIES[i]);
String[] testingFiles = classDir.list();
for (int j=0; j<testingFiles.length; ++j)
{
String text= Files.readFromFile(new File(classDir,testingFiles[j]),"ISO-8859-1");
System.out.print("\nTesting on " + CATEGORIES[i] + "/" + testingFiles[j] + " ");
Classification classification= new Classification(CATEGORIES[i]);
Classified<CharSequence> classified= new Classified<CharSequence>(text,classification);
evaluator.handle(classified);
JointClassification jc =compiledClassifier.classify(text);
String bestCategory = jc.bestCategory();
String details = jc.toString();
System.out.println("\tGot best category of: " + bestCategory);
System.out.println(jc.toString());
}}
}
}

Using opencsv - java.lang.OutOfMemoryError: Java heap space

I am using opencsv to parse two csv files. I only copy some values from the two files.
I have a seperate function which processes the CDax.csv. Which looks like that:
public HashMap<String,String> readCDax() throws Exception {
String csvDaxFile = "C:\\Users\\CDAX.csv";
CSVReader reader = new CSVReader(new FileReader(csvDaxFile), ';');
String [] line;
HashMap<String, String> cdaxMap = new HashMap<String, String>();
while ((line = reader.readNext()) != null) {
cdaxMap.put(line[0], line[7]);
}
System.out.println("Process CDax File!");
reader.close();
return cdaxMap;
}
My main method is run() which I execute in my main method:
public void run() throws Exception {
while ((firstLine = reader.readNext()) != null && (secondLine = reader.readNext()) != null && i<10) {
//fileName of the String
fileName = firstLine[0];
writerPath = "C:\\Users\\" + fileName + ".csv";
//write csv file
CSVWriter writer = new CSVWriter(new FileWriter(writerPath), ';');
//write Header
//String[] entries = "Name;Date;TotalReturn;Currency".split(";");
String [] entries = {"Name","Date", "TotalReturn", "Currency"};
writer.writeNext(entries);
//create Content
//companyName of the String
companyName = secondLine[1];
//currency
currency = secondLine[2];
//dates
dateList = new ArrayList<String>();
for(int p = 3; p < firstLine.length; p++) {
dateList.add(firstLine[p]);
}
//total returns
returnList = new ArrayList<String>();
for(int j = 3; j < secondLine.length; j++) {
returnList.add(secondLine[j]);
}
// cDaxList
cDaxList = new ArrayList<String>();
for(int j = 1; j <dateList.size(); j++) {
if(cDaxMethodValuesMap.containsKey(dateList.get(j))){
cDaxList.add(cDaxMethodValuesMap.get(dateList.get(j)));
} else{
dateList.add("na"); // I get the error here!
}
}
if(dateList.size()!=returnList.size()) {
System.out.println("Dates and Returns do not have the same length!");
}
int minSize = Math.min(dateList.size(), returnList.size());
//"Name;Date;TotalReturn;Currency"
List<String[]> data = new ArrayList<String[]>();
for(int m = 0; m < minSize; m++) {
data.add(new String[] {companyName, dateList.get(m), returnList.get(m), currency, cDaxList.get(m)});
}
writer.writeAll(data);
//close Writer
writer.close();
i++;
System.out.println(fileName + " parsed successfully!");
}
System.out.println("Done");
}
However when I run my program I get:
Process CDax File!
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at com.TransformCSV.main.ParseCSV.run(ParseCSV.java:109)
at com.TransformCSV.main.ParseCSV.main(ParseCSV.java:21)
I am getting the error in this method:
cDaxList = new ArrayList<String>();
for(int j = 1; j <dateList.size(); j++) {
if(cDaxMethodValuesMap.containsKey(dateList.get(j))){
cDaxList.add(cDaxMethodValuesMap.get(dateList.get(j)));
} else{
dateList.add("na"); //I get the error here!!!
}
}
I tried to put up the heapsize via the vm settings, however I do not think that this should be done because I only read in both csv files only 3000 values.
I appreciate your reply!
Your loop:
for(int j = 1; j <dateList.size(); j++) {
is looping through dateList but in that loop you are adding to dateList:
dateList.add("na"); //I get the error here!!!
so dateList will get bigger and bigger until you run out of memory. dateList.size() is evaluated every time through the loop, not once at the beginning.

Categories