Recently we added checking with Fortify to our code during build.
Following there are 2 lines of code, both lines cause 2 critical errors in Fortify: "Path manipulation".
Notice that I added the call to normalize() just to avoid that, and for the same reason I used a Path instead of String.
But now, instead of having 1 error as previously, I have 2 errors (in both lines).
Path normalizedPath = Paths.get(aPath + "/" + aStringFromDB + "/" + aFileName + ".txt").normalize();
BufferedOutputStream b = new BufferedOutputStream(new FileOutputStream(new File(String.valueOf(normalizedPath))));
What am I doing wrong?
It came out that the "normalize" of Path was not considered by Fortify.
The solution was to import and use the Apache normalize():
// import org.apache.commons.io.FilenameUtils;
String normalizedPath = FilenameUtils.normalize((aPath + "/" + aStringFromDB + "/" + aFileName + ".txt");
BufferedOutputStream b = new BufferedOutputStream(new FileOutputStream(new File(normalizedPath)));
Related
I have this simple code, but when I run, I get an error of not such a directory or file! how can I solved I tried many ways none of them works!! can anyone help?
public static void main (String [] args) {
String songA = ("res/raw/canon_d_major.wav");
String songB = ("res/raw/canon_d_major.wav");
Wave waveA = new Wave(songA);
Wave waveB = new Wave(songB);
String recordedClip = ("res/raw/cock_a.1.wav");
Wave waveRec = new Wave(recordedClip);
FingerprintSimilarity similarity1, similarity2;
similarity1= waveA.getFingerprintSimilarity(waveRec);
System.out.println("clip is found at " +
similarity.getsetMostSimilarTimePosition() + "s in " + songA +
" with similarity " + similarity.getSimilarity());
similarity2 = waveB.getFingerprintSimilarity(waveRec);
System.out.println("clip is found at " +
similarity.getsetMostSimilarTimePosition() + "s in " + songB +
" with similarity " + similarity.getSimilarity());
}
File fileName = new File('path/to/file'); does not create file on your hdd. Its only new File object in java that points to the dir you set in constructor. With that said if cock_a.wav doesnt exist while code is executing it wont be physically created.
Use this -> Java's createNewFile() - will it also create directories?
Also it might be helpful if you post your directory structure here.
I'm learning how we can access resources in java independently. So I have tried different ways as the listed below using File class.
System.out.println("File .: " + new File(".").getAbsolutePath());
System.out.println("File /: " + new File("/").getAbsolutePath());
System.out.println("File /.: " + new File("/.").getAbsolutePath());
System.out.println("File ./: " + new File("./").getAbsolutePath());
System.out.println("File ..: " + new File("..").getAbsolutePath());
System.out.println("File ../: " + new File("../").getAbsolutePath()); // why / not printed at the end?
System.out.println("File /..: " + new File("/..").getAbsolutePath());
System.out.println("File /../: " + new File("/../").getAbsolutePath()); // why / also not here
System.out.println("File //: " + new File("//").getAbsolutePath()); // why output is only \\ No path even
System.out.println("File ..//: " + new File("..//").getAbsolutePath()); // why not printed // at the end?
System.out.println("File //..//: " + new File("//..//").getAbsolutePath()); // why output \\.. only. No path even
Output is listed as below.
File .: D:\8th Semester\Java\CMDProjects\ClassPathTest\out\.
File /: D:\
File /.: D:\.
File ./: D:\8th Semester\Java\CMDProjects\ClassPathTest\out\. // I'm expecting / at the end too.
File ..: D:\8th Semester\Java\CMDProjects\ClassPathTest\out\..
File ../: D:\8th Semester\Java\CMDProjects\ClassPathTest\out\.. // same above question
File /..: D:\..
File /../: D:\.. // same above question
File //: \\ // Here why D:\ path not printed?
File ..//: D:\8th Semester\Java\CMDProjects\ClassPathTest\out\..
File //..//: \\.. //strange same above.
Then I tried same things with class's getResource() method. It works same as expected but on the last three lines I found exception by checking one by one Why?
URL dot = Test.class.getResource(".");
URL dotS = Test.class.getResource("./");
URL S = Test.class.getResource("/");
URL Sdot = Test.class.getResource("/.");
URL ddot = Test.class.getResource("..");
URL ddotS = Test.class.getResource("../");
URL sdd = Test.class.getResource("/..");
URL sdds = Test.class.getResource("/../");
URL ss = Test.class.getResource("//");
System.out.println("getR .: " + dot.toString());
System.out.println("getR /:" + S.toString());
System.out.println("getR /.:" + Sdot.toString());
System.out.println("getR ./:" + dotS.toString());
System.out.println("getR ..:" + ddot.toString());
System.out.println("getR ../:" + ddotS.toString());
// System.out.println("getR /..:" + sdd.toString()); // Exception Here
// System.out.println("getR /../:" + sdds.toString()); // Exception Here
// System.out.println("getR //:" + ss.toString()); // Exception Here
I would like to ask Why class's method getResource() throws NullPointer exception? and why the above File's output is different? see the top code where I wrote // why?
I'm using Windows 10.
As I already mentioned, I'm learning resource accessing different ways. Then if you have any useful links please share that too.
The null pointer exception is because Test.class.getResource() is returning null for those last three path strings. This happens whenever the indicated resource could not be found. For security reasons, Java code isn't allowed to access (or even know about) resources outside the class path. The path "/" maps to the root directory of the class path used to load class Test. It looks from your output that this is D:\8th Semester\Java\CMDProjects\ClassPathTest (or possibly D:\8th Semester\Java\CMDProjects\ClassPathTest\out; you don't report the output from the second part of your experiment).
As to why the trailing slash disappears from your file paths, that's part of how File#toString() is supposed to work. According to the docs, File#toString() returns the same string as File#getPath(). The docs for that method, in turn, say:
The resulting string uses the default name-separator character to separate the names in the name sequence.
Note that the name separator character is used to separate the names. It doesn't preserve trailing separators (that don't separate anything).
I'm not sure why the absolute path for "//..//" is "\\..". (Or, for that matter, why "//.." results in "\\". It's easy to understand why the trailing // is suppressed; those are name separator characters that don't separate anything. On my Mac the output for both of those is "/..". This behavior is obviously OS-dependent. I suspect that on Windows, the leading "//" represents a network path to a computer and the computer name is just absent.
I am trying to read a file inside my jar from another class inside the jar. However I am continually getting the same error: Caught: class java.io.FileNotFoundException while attempting to read metrics: metrics.yml
At first I had my code do something like this, assuming it was from the path of the class:
String yamlPath = ".." + File.separator + ".." + File.separator + ".." + File.separator + ".." + File.separator + "myYaml.yml";
InputStream in = new FileInputStream(new File(yamlPath));
InputStreamReader isr = new InputStreamReader(in);
BufferedReader input = new BufferedReader(isr);
yamlObj = (HashMap) javaYAML.load(input);
I also did this assuming it might take the path from the base of the jar:
String yamlPath = "myYaml.yml";
InputStream in = new FileInputStream(new File(yamlPath));
InputStreamReader isr = new InputStreamReader(in);
BufferedReader input = new BufferedReader(isr);
yamlObj = (HashMap) javaYAML.load(input);
I then noticed this thread How to read a file from jar in Java? and found out that I needed a "/" before my path. I tried both methods above using the slash as well.
String yamlPath = File.seperator + ".." + File.separator + ".." + File.separator + ".." + File.separator + ".." + File.separator + "myYaml.yml";
OR
String yamlPath = File.seperator + "myYaml.yml";
I am completely lost on what to do about this error now. Is there something I am not getting about the jar structure? Why can my file not be found. Thanks in advance for any help / information.
Sorry, I forgot to mention where it is in the JAR:
The class is in the following path: com/a/b/c/myclass.class
The yaml is in the following path: myYaml.yml
File inside Jar is no longer a File, Change inputStream creation with this
InputStream in = YourClass.class.getResourceAsStream("myYaml.yml");
Assuming your .yml file is at root of jar
InputStream is = this.getClass().getClassLoader().getResourceAsStream("file");
Somehow for me, i have to add '/' in front of the file name.
InputStream resourceAsStream = Xx.class.getResourceAsStream("/test.yml");
From java doc:
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').
And after i checked more detailed information, my solution is same as
InputStream in = Xx.class.getClassLoader().getResourceAsStream("test.yml");
I am trying to read a file inside my jar from another class inside the jar. However I am continually getting the same error: Caught: class java.io.FileNotFoundException while attempting to read metrics: metrics.yml
At first I had my code do something like this, assuming it was from the path of the class:
String yamlPath = ".." + File.separator + ".." + File.separator + ".." + File.separator + ".." + File.separator + "myYaml.yml";
InputStream in = new FileInputStream(new File(yamlPath));
InputStreamReader isr = new InputStreamReader(in);
BufferedReader input = new BufferedReader(isr);
yamlObj = (HashMap) javaYAML.load(input);
I also did this assuming it might take the path from the base of the jar:
String yamlPath = "myYaml.yml";
InputStream in = new FileInputStream(new File(yamlPath));
InputStreamReader isr = new InputStreamReader(in);
BufferedReader input = new BufferedReader(isr);
yamlObj = (HashMap) javaYAML.load(input);
I then noticed this thread How to read a file from jar in Java? and found out that I needed a "/" before my path. I tried both methods above using the slash as well.
String yamlPath = File.seperator + ".." + File.separator + ".." + File.separator + ".." + File.separator + ".." + File.separator + "myYaml.yml";
OR
String yamlPath = File.seperator + "myYaml.yml";
I am completely lost on what to do about this error now. Is there something I am not getting about the jar structure? Why can my file not be found. Thanks in advance for any help / information.
Sorry, I forgot to mention where it is in the JAR:
The class is in the following path: com/a/b/c/myclass.class
The yaml is in the following path: myYaml.yml
File inside Jar is no longer a File, Change inputStream creation with this
InputStream in = YourClass.class.getResourceAsStream("myYaml.yml");
Assuming your .yml file is at root of jar
InputStream is = this.getClass().getClassLoader().getResourceAsStream("file");
Somehow for me, i have to add '/' in front of the file name.
InputStream resourceAsStream = Xx.class.getResourceAsStream("/test.yml");
From java doc:
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').
And after i checked more detailed information, my solution is same as
InputStream in = Xx.class.getClassLoader().getResourceAsStream("test.yml");
I am looking to get similar behaviour to what you get in Windows when you copy and paste a file in the same directory.
For e.g, if you've copy/paste a file called foo.txt, it will create foo Copy.txt and if you paste it once more, it creates foo Copy(2).txt and if you copy/paste foo Copy.txt, foo Copy Copy.txt is created.
Is there a Java utility function that does this? I've looked at File.createTempFile but the filename it generates is too long and contains a UID-like substring.
By using the FileChooser in combination with the "showSaveDialog"-method you will get the result you want, because java is then using the OS behaviour for existing files.
Sometimes, you just have to do the work first, it will give you an appreciation for the API. Then you can write your own utility methods
File original = new File("build.xml");
String path = original.getAbsoluteFile().getParent();
String name = original.getName();
String ext = name.substring(name.indexOf("."));
name = name.substring(0, name.indexOf("."));
name = path + File.separator + name;
int index = 1;
File copy = new File(name + " (" + index + ")" + ext);
while (copy.exists()) {
index++;
copy = new File(name + " (" + index + ")" + ext);
}
System.out.println(copy);