Tuesday, January 16, 2007

Where To Buy Tire Chains In Vancouver

Lee getResource ()

This example shows how to read a text file using "getResource ()."
For those already used getResource and collided with the problem by showing the path they came out something like this:
/ c: / Program% 20Files / ...
The idea to be taken into account is that the addresses "URL" does not contain spaces, so are stream replaces the character "% 20", this will repair the path using replaceAll ("% 20", "")
.

Note: The example shown here reads the file "getTextResource.java" and it shows.
I hope and will be useful.


import java.io. *;
import java.net .*;

/ *

* * @ author Oscar Hurtado Morato

* * /
getTextResource {public class public
getTextResource () {
}
public static void main(String[] arg) {
try{
getTextResource ex=new getTextResource();
ex.leeArchivo();
}
catch(Exception e){
e.printStackTrace();
}
}
public void leeArchivo()throws IOException {
URL url=this.getClass().getResource("getTextResource.java");
String path=url.getPath();
path=path.replaceAll("%20"," ");
System.out.println(path);
/ / read the file
BufferedReader br = new BufferedReader (new FileReader (path));
thisLine String = "";
retDoc String = "";
while ((thisLine = br.readLine ())! = null) {
thisLine retDoc = retDoc + + "\\ n";}

System.out.println (retDoc)

}}

0 comments:

Post a Comment