So I have a method that reads all the files in a folder and creates new classes in a List with the variables read from the files. For some reason it doesn't ever get past the if(mainDir.isDirectory()){ part, even though the paths are correct and I double checked the folders were there.
public static void loadIntoClass(String dir, int temp){
try {
File mainDir = new File(dir);
if(mainDir.isDirectory()){ //Checks if the dir is a folder and not a file
String[] fileNames = mainDir.list(); //Grabs a list of all the filenames in the dir
for(int x = 0; x > fileNames.length; x++){ //loops through all the files
File currFile = new File(dir + fileNames[x]); //Creates the object we will be gathering information from
if(currFile.isFile()){ //Checks to make sure the file is a file and not a folder
BufferedReader br = new BufferedReader(new FileReader(currFile));
String line = br.readLine();
int currLoop = 1;
boolean collides = false;
while(line != null){ //Will keep checking files until it reaches a blank line
currLoop ++; //Keeps track of how many times it loops
test = line.split("="); //Splits up the variable from the declaration
String toString = test[0].trim(); //Trims off any extra blank spaces on either side
System.out.println("Reading: " + toString + " on line " + currLoop); //For debugging
String toString2 = test[1].trim(); //Trims the second string
parse[currLoop] = Integer.parseInt(toString2); //Turns the string into an integer then puts it into the array
if(toString.equalsIgnoreCase("Collides")){
if(toString2.equalsIgnoreCase("true")){
collides = true;
}
}
if(toString.equalsIgnoreCase("Image Path")){
//path = toString2;
}
line = br.readLine();
}
if(temp == 1){
types.add(new Type(parse[1], parse[2], parse[3], parse[4], parse[5], parse[6], parse[7]));
}
if(temp == 2){
tiles.add(new Tiles(parse[1], collides, null));
}
if(temp == 3){
abilities.add(new Abilities(parse[1], parse[2], parse[3], parse[4]));
}
br.close();
}
}
}
} catch(Exception e) {
System.err.println("ERROR: " + e);
}
}
After that if I change it some other path like "C:/test" it works only to freeze at the for loop. Here's the declaration:
loadIntoClass("C:/Program Files(x86)/GameNameHere/config/enemies", 1);
The methods isDirectory() and isFile() doe not work if the underlying FS-Objects do not exist.
There are multiple possible issues, which you are not taking into consideration...
Your not checking to see if the dir exists
Your not making sure to close your files in case of an read error (or other associated error)
You making life tough for yourself using File#list, instead use File#listFiles which will return an array of File
Make better use of exceptions...
For example...
public static void loadIntoClass(String dir, int temp) throws IOException {
File mainDir = new File(dir);
if(mainDir.exists) { // Check to see if the abstract path actually exists
if (mainDir.isDirectory()){ //Checks if the dir is a folder and not a file
File[] fileNames = mainDir.listFiles(); //Grabs a list of all the filenames in the dir
//String[] fileNames = mainDir.list(); //Grabs a list of all the filenames in the dir
if (fileNames != null && fileNames.length > 0) {
//for(int x = 0; x > fileNames.length; x++){ //loops through all the files
for(File currFile : fileNames){ //loops through all the files
//File currFile = new File(dir + fileNames[x]); //Creates the object we will be gathering information from
if(currFile.isFile()){ //Checks to make sure the file is a file and not a folder
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(currFile));
String line = null;
int currLoop = 1;
boolean collides = false;
while((line = br.readLine()) != null){ //Will keep checking files until it reaches a blank line
//...//
}
//...//
// Make sure you make all best attempts to close the reader...
} finally {
try {
br.close();
} catch (Exception exp) {
}
}
}
}
} else {
// You may not care, but...
throw new IOException(dir + " does not contain any files");
}
} else {
throw new IOException(dir + " is not a directory");
}
} else {
throw new IOException(dir + " does not exist");
}
}
Related
I have loaded from text files many times before without thi issue, I have read and re-read my code and I (personally) cant see why I would get this issue, I am completely lost.
static public ArrayList<Media> importMedia(String fileName) throws IOException
{
try {
ArrayList<Media> mList = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
int numberOfItems = Integer.valueOf(line);
while((line = reader.readLine()) != null)
{
String[] split = line.split(",");
if(split[0].contains("mp3"))
{
Mp3 mp3 = new Mp3(split[1]/*title*/,split[0]/*filename*/,Integer.parseInt(split[4])/*releaseyear*/,split[2]/*artist*/,split[3]/*album*/,split[5]/*label*/,Double.parseDouble(split[6])/*runtime*/);
mList.add(mp3);
}else if (split[0].contains("gif"))
{
Gif gif = new Gif(split[1]/*title*/,split[0]/*filename*/,Integer.parseInt(split[6])/*releaseyear*/,Double.parseDouble(split[2])/*width*/,Double.parseDouble(split[3])/*height*/,split[4]/*equipName*/,split[5]/*equipModel*/);
mList.add(gif);
}else if(split[0].contains("avi"))
{
String castNames = "";
boolean first = true;
for(int i = 7; i < 15; i++)
{
if(!(split[i].isEmpty()))
{
if(first)
{
castNames += split[i];
first = false;
}else{
castNames += "," + split[i];
}
}
}
Avi avi = new Avi(split[1]/*title*/,split[0]/*filename*/,Integer.parseInt(split[3])/*releaseyear*/,split[2]/*studio*/,split[5]/*director*/,castNames/*castnames*/,Double.parseDouble(split[4])/*runtime*/,Integer.parseInt(split[6])/*cast*/);
mList.add(avi);
}else{
}
}
return mList;
} catch (Exception ex) { System.out.println(ex.toString()); }
return null;
}
Now it will only get the first 3 files(Console shown in picture)
I am simply trying to loop through and I am not sure why it would be out of bounds, I cannot see anything wrong with the loop, or why its giving me some but not all.
In this code you are using a String Array split from index 0 to index 14.
It would be good to do some defensive programming by checking length of String Array.
Please check the length of array before proceeding to use it in your programme.
like split.length >14
By using this habit you can always escape from 'ArrayIndexOutOfBoundsException'
I have a Method that calls a second method, the second method will:
Create any missing directories
Create a file
Decode a 2D String[] to a String (Not working)
Write content
Write the decoded String to the file with a header (Not working)
First method
public static boolean store(Exception exception, String[][] flags){
return storePrivate(exception, location, flags);
}
Second Method (Not all code just relevant code)
private static boolean storePrivate(Exception exception, String dir, String[][] flags){
String flag = "";
for(int i = 0; i >= flags.length; i++){
flag = flag + "" + flags[i][0] + ": " + flags[i][1] + "\n";
}
try {
File directory = new File(dir);
File file = new File(dir + id + ".txt");
if (!directory.exists()) {
directory.mkdirs();
}
file.createNewFile();
FileWriter filewriter = new FileWriter(file.getAbsoluteFile());
BufferedWriter writer = new BufferedWriter(filewriter);
if(flag != ""){
writer.write("Flags by Developer: ");
writer.write(flag);
}
writer.flush();
writer.close();
return true;
} catch (IOException e) {
return false;
}
}
Call to the first method
public static void main(String[] args) {
try {
test();
} catch (Exception e) {
// TODO Auto-generated catch block
ExceptionAPI.store(e, new String[][]{{"flag1", "Should be part of flag1"}, {"flag2", "this should be flag 2 contence"}});
}
}
public static void test() throws IOException{
throw new IOException();
}
I cant find why this won't work. I think it has to do with the second method, particularly
if(flag != ""){
writer.write("Flags by Developer: ");
writer.write(flag);
}
Thanks if anyone can help me.
Curlip
Try this if you want to just convert an array of strings into a single string:
String[] stringTwoD = //... I think this is a 1D array, and Sting[][] is a 2D, anyway
String stringOneD = "";
for (String s : stringTwoD)
stringOneD += s;//add the string with the format you want
BTW, your loop condition seems wrong and ,so you may change it to :
for(int i = 0; i < flags.length; i++){
flag += flags[i][0] + ": " + flags[i][1] + "\n";
}
I want to display some of the content of the .txt file on the screen after copying it into a new file. The content of the text file is not exactly the same structure throughout.
When i did this
if (m[11].equals("Channel") && m[12].equals("ID")){ System.out.println("Wavelenght ID = " + m[9]);
I got an error array out of bounds.
TESTSTEP: DEBUG * Fpga Config ECFG *: EED 3 : Channel ID
If this line was found i want it to display Wavelenght ID = 3
String p = path.replace("\\", "\\\\");
File file = new File(p);
File[] files = file.listFiles();
BufferedReader inputStream = null;
PrintWriter outputStreamI = null;
PrintWriter outputStreamO = null;
try {
String l,c;
for (int i=0; i<files.length; i++){
inputStream = new BufferedReader(new FileReader(files[i].getAbsolutePath()));
outputStreamI = new PrintWriter(new FileWriter("S:\\WRED_IBP\\" + files[i].getName().replaceFirst("[.][^.]+$", "")+ "Ingress.txt"));
outputStreamO = new PrintWriter(new FileWriter("S:\\WRED_IBP\\" +files[i].getName().replaceFirst("[.][^.]+$", "")+ "Egress.txt"));
while ((l = inputStream.readLine())!= null) {
String[] m=l.split(" ");
for(int d=0; d<m.length; d++){
c=m[d];
if (c.equalsIgnoreCase("ied_wred") ){outputStreamI.println(l); if (outputStreamI.checkError()){System.err.println("Error in output stream");}} //|| c.equalsIgnoreCase("WARNING") || c.equals("ERROR")
else if (c.equalsIgnoreCase("eed_brc_ibp")){outputStreamO.println(l); if (outputStreamO.checkError()){System.err.println("Error in output stream");}} // || c.equalsIgnoreCase("WARNING") || c.equals("ERROR")
}
}
}
} catch(IOException e){
System.err.println("Caught IOException: " + e.getMessage());
}//wait(10);
finally {
if (inputStream != null) {
try{ inputStream.close();} catch (IOException e) {e.printStackTrace();}}
if (outputStreamI != null) {outputStreamI.close();}
if (outputStreamO != null) {outputStreamO.close();}
}
}
You can use:
if (m.length > 12 && m[11].equals("Channel") && m[12].equals("ID")){
System.out.println("Wavelenght ID = " + m[9]);
}
So index out of bound won't happen here.
Make sure you put these lines before the for loop.
TESTSTEP: DEBUG * Fpga Config ECFG *: EED 3 : Channel ID
I'm assuming this is the text in the file. When you read this, you are trying to split it based on spaces. What you should be doing is split it based on :.
import java.io.*;
import java.io.File;
import java.io.FilenameFilter;
public class YDSearch{
public void listFiles(String dir) throws IOException{
File directory = new File(dir);
if (!directory.isDirectory()) {
System.out.println("No directory provided");
return;
}
//create a FilenameFilter and override its accept-method
FilenameFilter filefilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
//if the file extension is .mp3 return true, else false
return name.endsWith(".mp3")||name.endsWith(".mp4")||name.endsWith(".3gp")
||name.endsWith(".mov")||name.endsWith(".avi")||name.endsWith(".wmv");
}
};
String[] filenames = directory.list(filefilter);
DataOutputStream output = new DataOutputStream(new FileOutputStream("C:/Users/Jonathan/Desktop/YouDetect/SearchByFileType/AllMediaFiles.dat"));
for (String name : filenames) {
output.writeUTF(dir + name);
}
output.close();
DataInputStream input = new DataInputStream(new FileInputStream("C:/Users/Jonathan/Desktop/YouDetect/SearchByFileType/AllMediaFiles.dat"));
DataOutputStream output2 = new DataOutputStream(new FileOutputStream("C:/Users/Jonathan/Desktop/ReadyForAnalysis.dat"));
for (String name : filenames) {
FileInputStream in = new FileInputStream(input.readUTF());
int byteCounter = 0;
int rowCounter = 0;
long bufferCounter = 0;
if(name.endsWith(".mp3")){
byte[] b = new byte[36];
int read = in.read(b, 0, 36);
if (byteCounter != 1000){
if (rowCounter == 1){
System.out.println("\n");
rowCounter = 0;
}
output2.writeUTF(org.apache.commons.codec.binary.Hex.encodeHexString(b)+ " " + dir + name);
bufferCounter ++;
rowCounter ++;
}else{
byteCounter = 0;
try{
Thread.sleep(200);
}catch(InterruptedException e) {
}
}
}
else if(name.endsWith(".mp4")){
byte[] b = new byte[29];
int read = in.read(b, 0, 29);
if (byteCounter != 1000){
if (rowCounter == 1){
System.out.println("\n");
rowCounter = 0;
}
output2.writeUTF(org.apache.commons.codec.binary.Hex.encodeHexString(b)+ " " + dir + name);
bufferCounter ++;
rowCounter ++;
}else{
byteCounter = 0;
try{
Thread.sleep(200);
}catch(InterruptedException e) {
}
}
}
//System.out.println("====================");
}
output2.close();
input.close();
DataInputStream input2 = new DataInputStream(new FileInputStream("C:/Users/Jonathan/Desktop/ReadyForAnalysis.dat"));
for (String name : filenames) {
System.out.println(input2.readUTF()+"\n");
}
}
public void checkHeaderSC(String allFiles)throws IOException{
}
public static void main(String[] args) throws IOException {
YDSearch YDSearch = new YDSearch();
YDSearch.listFiles("C:/Users/Jonathan/Desktop/YD Tests/1) High Quality/");
YDSearch.listFiles("C:/Users/Jonathan/Desktop/YD Tests/2) Medium Quality/");
YDSearch.listFiles("C:/Users/Jonathan/Desktop/YD Tests/3) Low Quality/");
YDSearch.checkHeaderSC("C:/Users/Jonathan/Desktop/YouDetect/SearchByFileType/ReadyForAnalysis.dat");
}
}
Hey there, having a little issue with the above coding and hoped someone here might be able to help. This is sort of a partial version of the code as the real one has 4 more if/else if statements involved.
The program compiles and begins to run fine. It produces several results back from the file that is being read into/then out of again in input2 but then stops, produces no more results and gives the error:
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:323)
at java.io.DataInputStream.readUTF(DataInputStream.java:572)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at YDSearch.listFiles(YDSearch.java:85)
at YDSearch.main(YDSearch.java:93)
Anybody know why this might be happening and have a solution they could share?
I've also tried making the variable 'b' to be inside of an if statement but that doesn't work because of scope. If b was defined by if's then there would only need to be one if statement to output to the file
Please let me know if you've got any ideas, I'd really appreciate it :)
As far as I can see, you don't always put out an output record for every name, only for when the name matches one of your patterns. However, you do try to read an input record for every name.
Ergo, if you have any filenames that don't match the patterns you try to read more than you write, and you will get the EOF.
EDIT:
In more detail, the problem is that you get a list of all the files that end with "mp3", "mp4", "3gp", "mov", "avi or "wmv". You then process that list, and write out something into C:/Users/Jonathan/Desktop/ReadyForAnalysis.dat for each "mp3" and "mp4" file. You then assume that for each entry in your list of files, that you will have an entry in ReadyForAnalysis.dat. However, if there are any files ending in "3gp", "mov", "avi or "wmv" then this will not hold true.
I am coding a class that compares the files of two directories via comparing the Byte arrays of each file. I am however not getting the expected results; identical files are not being resolved as identical files.
First problem:
Matching the files Byte[] with equals() resolves to false with matching files (Checked with only one file as to circumvent the possible index misalignment issue; the check still resolves to false.).
Second problem:
When using Vector's containsAll() for checking that both Vectors of Byte[] match (One Vector per directory with Byte[] for each file) this check results in false even with identical directories (This check has been removed from the code below.). So is there an issue with the way I am aligning the two vectors? (I have checked this with using two directories with matching files in the same order loaded into matching indeces; this still results in a Vector mismatch).
Third problem:
When there are subdirectories in the directories being checked a file not found exception is thrown stating that access is denied. Why is this happening? How can I circumvent this? I do not want to check the files contained within the subdirectories, but I am designing the code so that the end user need not worry about the subdirectories of the directories being compared. This only happens when there are subdirectories, it work fine when there are no subdirectories in the directories being checked.
Example Exception:
Byte reading error!
Byte reading error!
java.io.FileNotFoundException: C:\Dir1\Dir2\Dir3\Dir4\SubDir (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at tools.filesystem.filecomparison.FileComparator.getBytes(FileComparator.java:166)
at tools.filesystem.filecomparison.FileComparator.main(FileComparator.java:102)
java.io.FileNotFoundException: C:\Dir1\Dir2\Dir3\Dir4\SubDir Files (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at tools.filesystem.filecomparison.FileComparator.getBytes(FileComparator.java:166)
at tools.filesystem.filecomparison.FileComparator.main(FileComparator.java:111)
Here is the code:
package tools.filesystem.filecomparison;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import java.util.Vector;
public class FileComparator
{
public static void main(String[] args)
{
String workingDir1 = "";
String workingDir2 = "";
File[] fileArr1 = null;
File[] fileArr2 = null;
Vector<File> fileVec1 = new Vector<File>();
Vector<File> fileVec2 = new Vector<File>();
Scanner console = new Scanner(System.in);
while (true)
{
System.out.println("Enter working directory one . . . .");
workingDir1 = console.nextLine();
workingDir1.replace("\\", "\\\\");
System.out.println("Enter working directory two . . . .");
workingDir2 = console.nextLine();
workingDir2.replace("\\", "\\\\");
File folder1 = new File(workingDir1);
File[] listOfFiles1 = folder1.listFiles();
File folder2 = new File(workingDir1);
File[] listOfFiles2 = folder2.listFiles();
fileArr1 = listOfFiles1;
fileArr2 = listOfFiles2;
System.out.println("\nWorking Directory 1 Files\n");
for (int i = 0; i < listOfFiles1.length; i++)
{
if (listOfFiles1[i].isFile())
{
System.out.println(" " + listOfFiles1[i].getName());
}
/* else if (listOfFiles1[i].isDirectory())
{
System.out.println("Directory " + listOfFiles1[i].getName());
}*/
}
System.out.println("\nWorking Directory 2 Files\n");
for (int i = 0; i < listOfFiles2.length; i++)
{
if (listOfFiles2[i].isFile())
{
System.out.println(" " + listOfFiles2[i].getName());
}
/* else if (listOfFiles2[i].isDirectory())
{
System.out.println("Directory " + listOfFiles2[i].getName());
}*/
}
for (File fle : fileArr1)
{
fileVec1.add(fle);
}
for (File fle : fileArr2)
{
fileVec2.add(fle);
}
if (fileVec1.containsAll(fileVec2))
break;
else
{
System.out.println("Directories do not contain the same files!\nContinue anyways? y/n?");
if (console.nextLine().equalsIgnoreCase("y"))
break;
else if (console.nextLine().equalsIgnoreCase("n"))
continue;
}
}
Vector<Vector<File>> alignedVectors = align(fileVec1, fileVec2);
fileVec1 = alignedVectors.elementAt(0);
fileVec2 = alignedVectors.elementAt(1);
Vector<byte[]> fileByteVect1 = new Vector<byte[]>();
Vector<byte[]> fileByteVect2 = new Vector<byte[]>();
try
{
fileByteVect1 = getBytes(fileVec1);
}
catch (IOException e)
{
System.out.println("Byte reading error!");
e.printStackTrace();
}
try
{
fileByteVect2 = getBytes(fileVec2);
}
catch (IOException e)
{
System.out.println("Byte reading error!");
e.printStackTrace();
}
boolean[] check = new boolean[fileByteVect1.capacity()];
int i1 = 0;
//debug
for (byte[] e : fileByteVect1)
{
System.out.println("Vector 1 count " + i1);
System.out.println(e.toString());
for (byte b : e)
{
System.out.print(b + " ");
}
i1++;
}
int i2 = 0;
//debug
for (byte[] e : fileByteVect2)
{
System.out.println("Vector 2 count " + i2);
System.out.println(e.toString());
for (byte b : e)
{
System.out.print(b + " ");
}
i2++;
}
if (fileByteVect1.size() == fileByteVect2.size())
{
System.out.println(fileByteVect1.size());
for (int i = 0; i < fileByteVect1.size(); i++ )
{
if (fileByteVect1.elementAt(i).equals(fileByteVect2.elementAt(i)))
{
check[i] = true;
System.out.println("File at index " + i + " are identical");
}
else
{
check[i] = false;
System.out.println("File at index " + i + " are not identical");
}
}
}
else
System.out.println("Files do not match!");
}
public static Vector<Vector<File>> align(Vector<File> fileVect1, Vector<File> fileVect2)
{
Vector<Vector<File>> mainBuffer = new Vector<Vector<File>>();
Vector<File> bufferFileVect = new Vector<File>();
for (File fle1 : fileVect1)
{
for (File fle2 : fileVect2)
{
if (fle1.getName().equals(fle2.getName()))
bufferFileVect.add(fle2);
}
}
mainBuffer.add(fileVect1);
mainBuffer.add(bufferFileVect);
return mainBuffer;
}
public static Vector<byte[]> getBytes(Vector<File> fileVector) throws IOException
{
Vector<byte[]> outVector = new Vector<byte[]>();
for (File file : fileVector)
{
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
if (length > Integer.MAX_VALUE)
{
System.out.println("File is too large!");
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0)
{
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length)
{
throw new IOException("Could not completely read file " + file.getName());
}
// Close the input stream and return bytes
outVector.add(bytes);
is.close();
}
return outVector;
}
}
The equals function isn't doing a deep comparison, rather for a byte[] you're comparing addresses. Instead you should use
Arrays.equals(fileByteVect1.elementAt(i), fileByteVect2.elementAt(i))
to perform the deep comparison of the byte arrays.
More detail on Arrays.equals.
As for your third question, you're not actually filtering for just files. When you iterate through to print out the filename you should construct the Vector storing the files:
for (File fle : fileArr1) {
if (fle.isFile()) {
fileVec1.add(fle);
System.out.println(" " + fle.getName());
}
}
You will, of course, have to do this for fileArr2 and fileVec2 as well.
Simple. The equals(Object) method on an array is inherited from Object, and hence is equivalent to the == operator; i.e. it is just a reference comparison.
This is specified in JLS 6.4.5.
If you want to compare arrays by value, use the java.util.Arrays.equals(array1, array2) methods. There are overloads for arrays of each primitive type and arrays of Object.
(Note that it is the semantics of each element type's implementation of equals method that determines if Arrays.equals(Object[], Object[]) is a "deep" or "shallow" comparison.)
FOLLOW UP
I suspect that the third problem happens because your application is trying to open the subdirectory as a file. That won't work. Instead, you need to:
Use File.isFile() and File.isDirectory() to determine whether you should be reading the directory entries as files or dirctories (or not at all).
For a directory, you should recursively use File.listFiles() or similar to iterate over the subdirectory contents.