Sonar: visitFile(): How to get the source code file - java

I'm implementing custom script rule plugin for Sonar.
I want to make a checking rule directly for the source code
and not from checking tokens or nodes of the ASTtree.
Having the follow code:
#Override
public void visitFile() {
BufferedReader br = null;
File file = null;
String line = null;
try {
file = this.getSourceCode().getFile();
br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
...
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
My problem is that the :
this.getSourceCode().getFile();
returns back null
how can I get the instance of the file for which was actually the visitFile() called?
How does 'visitFile()' works actually?

Related

Java read json file from another jar

so Im trying to develop a "module/addon" system and I want to read a "module.json" file from all modules/addons loaded in a direcory. So I got the part with looping through the files and stuff. But I dont know how I can access that "module.json" file.
Here is what I tried:
try {
for (File file : this.modulesDir.listFiles()) {
if(file.getName().endsWith(".jar")) {
InputStream stream = new URLClassLoader(new URL[]{file.toURL()}).getResourceAsStream("./module.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
while((builder.append(reader.readLine())) != null);
reader.close();
stream.close();
System.out.println(builder.toString());
}
}
} catch (IOException e) {
e.printStackTrace();
}
Alright! I fixed it by changeing the path of the file to module.json instead of /module.json
Heres the code:
try {
for (File file : this.modulesDir.listFiles()) {
if(file.getName().endsWith(".jar")) {
InputStream stream = new URLClassLoader(new URL[]{file.toURL()}).getResourceAsStream("module.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
builder.append(line);
}
System.out.println(builder.toString());
}
}
} catch (IOException e) {
e.printStackTrace();
}

Best way to read data from 16MB file without freezing the app

I have a problem and can't find a good solution to it. I need to read a textfile with large amount of data (file has 16MB). The file contains 12 columns with integer values in each one. Generally my problem is how to do this without freezing the app. I have my file in the assets folder of my project and I tried using something like this:
AssetManager assetManager = this.getAssets();
try {
InputStream inputStream = assetManager.open("3333.ecg");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String str;
while ((str = bufferedReader.readLine()) != null) {
stringBuilder.append(str);
}
} catch (IOException e) {
e.printStackTrace();
}
But app freezes. My goal is to get the data from each column and save it into an arraylist of integers. I'm looking for some advice.
Thanks in advance.
Use this to run it in background:
AsyncTask.execute(new Runnable() {
#Override
public void run() {
AssetManager assetManager = this.getAssets();
try {
InputStream inputStream = assetManager.open("3333.ecg");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String str;
while ((str = bufferedReader.readLine()) != null) {
stringBuilder.append(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});

How to change address in url (http://localhost:8080/HELLO_WORLD) in NanoHttpd

My query is how to change how to change address in URL (http://localhost:8080/HELLO_WORLD). I change HELLO_WORLD to desire word.
#Override
public Response serve(IHTTPSession session) {
String answer = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(appContext.getAssets().open("block.html")));
// do reading, usually loop until end of file reading
String mLine;
while ((mLine = reader.readLine()) != null) {
//process line
answer += mLine;
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
Log.d("BABAR", "EXception occured in serve()");
}
}
}
return newFixedLengthResponse(answer);
}
please suggest me how to change
I donĀ“t know if this is what you want, but you can try.
You have to follow the steps:
1- Create a local to store your server files;
2-Then change the response in the class that is implementing the NanoHttp server to something like this:
#Override
public Response serve(IHTTPSession session) {
String answer = "";
try{
FileReader filereader = new FileReader(contextoMain.local(localyourstorethefiles)+"/yourfolder/yourfile.html");
BufferedReader reader = new BufferedReader(filereader);
String line = "";
while ((line = reader.readLine()) != null) {
answer += line;
}
reader.close();
}catch(IOException ioe) {
Log.w("Httpd", ioe.toString());
}
return newFixedLengthResponse(answer);
}
3 - Then, call the localhost:8080 without putting the 8080/yourfolder/yourfile

String and Text File Match Very basic

So i have recently dove head first into java trying to learn lots of things. Recently i have been studying File Writers and Reader and Buffered Writers and Readers. I have recently came to a haul though every time i turn on the application the text file is modified agian. Is there a way that my text file can update every time i change a string. So basically it will read the file on boot and compare it to the string.
Here is my example of reading a text file and turning it into a string
private void Read() {
try(BufferedReader br = new BufferedReader(new FileReader(version))) {
String sCurrentLine;
while((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch(IOException e) {
e.printStackTrace();
}
}
All i need to do is it to compare this string with another. Thank you for your time.
** Update**
So i compared the two threads and now nothing is being written to the text file
private void Update() {
try {
fw= new FileWriter(version.getAbsoluteFile());
bw = new BufferedWriter(fw);
try(BufferedReader br = new BufferedReader(new FileReader(version))) {
String sCurrentLine;
while((sCurrentLine = br.readLine()) != null) {
if(!sCurrentLine.equals(VanoEngine.TITLE)) {
bw.write(VanoEngine.TITLE);
}
}
} catch(IOException e) {
e.printStackTrace();
}
}
catch(IOException e) {
e.printStackTrace();
}
}
** Update**
Closed the stream #JavaNoob and still nothing is being written
private void Update() {
try {
fw= new FileWriter(version.getAbsoluteFile());
bw = new BufferedWriter(fw);
try(BufferedReader br = new BufferedReader(new FileReader(version))) {
String sCurrentLine;
while((sCurrentLine = br.readLine()) != null) {
if(!sCurrentLine.equals(VanoEngine.TITLE)) {
bw.write(VanoEngine.TITLE);
}
}
bw.close();
} catch(IOException e) {
e.printStackTrace();
}
}
catch(IOException e) {
e.printStackTrace();
}
}
if(!sCurrentLine.equals(someInputString))
//write `someInputString` to file, because it differs with the one read from file
you can use sCurrentLine.equals("your string").

Reading different file one after another using BufferedReader in Java

I am using BufferedReader to read file in java.
Following is the code snippet:
String line;
br = new BufferedReader(new FileReader("file1.txt"));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
//Here I want to open file named "file2.txt".
As mentioned in the code snipped above, i want to now open a new file.
What is the best way to do so ?
Should i first close br using br.close, and then again initialise br or what ?
P.S.: I am new to Java.
Creating a method will make your code modular and easy to use. This will lead to re-usability of code and ease of understanding. Here is the sample code:
public static void main(String args[]) {
readFile("C:\\sample.txt");
}
public static void readFile(String filename) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(filename));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
I would make a method and call it twice
void readFile(String fileName) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
...
}
}
note that BufferedReader instance br will be closed automatically and make sure that you are using JDK 7 for this
File I/O operations internally use Decorator pattern. So, .close() on the outermost object should close all internal I/Os
Yes, close it first, but use this pattern ...
BufferedReader br = null;
String line;
try {
br = new BufferedReader(new FileReader("file1.txt"));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
finally {
if (br != null) br.close();
}
//Here I want to open file named "file2.txt".
Or, the try-with-resources approach (semantically equivalent to the above) ...
String line;
try (BufferedReader br = new BufferedReader(new FileReader("file1.txt")) {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
//Here I want to open file named "file2.txt".

Categories