I am trying to access a file using the file class but it works only with below code
File file = new File("s:\\testing\\selenium\\Version8\\locators\\OR.properties");
however I need to use this approach where I go directly to the server:
"\\GIGABYTE\\s-drive\\Testing\\selenium\\Version8\\locators\OR.properties"in which the above will not find the file - very strange
Any clue as to why this is?
To go directly to the server you will need to have 4 slashes as two are actual slashes and 2 are escape characters:
\\\\GIGABYTE\\s-drive\\Testing\\selenium\\Version8\\locators\OR.properties
You can try the below code:
File file = new File("\\\\GIGABYTE\\s-drive\\Testing\\selenium\\Version8\\locators\\OR.properties");
or this:
File file = new File("//GIGABYTE/s-drive/Testing/selenium/Version8/locators/OR.properties");
Related
I am new to Java.
While I am trying to copy a file using "File.copy" method in java.
The process end without errors.
But the file I need to copy is created with only one weired character (I think it is an file end character). and it has no content other than it.
Here is my source code,
var sourceFile = new File("mySourceFile.txt")
var destinyFile = new File("myDestinyFile.txt");
Files.copy(sourceFile.toPath(), destinyFile.toPath());
it doesnt matter if the file you will copy has one or hundred character. You say the process ends without errors. Does it mean it works fine or it does someting wrong but you get no error. If this is the case you can try to take the full path like this:
var sourceFile = new File("C:\\Users\\Kevin\\test1.txt").toPath();
var destinyFile = new File("C:\\Users\\Kevin\\test2.txt").toPath();
Files.copy(sourceFile, destinyFile);
I solved the problem.
The problem is that there are outputstreams already open for the same destiny file I have to.
So it File.copy() method cannot write on the file.
I'm loading some network path in my java code. It is not taking the same format as present in the configuration file, missing one slash.
Example:
String path = "//abckatte.com/abc/test";
File fileobj = new File(path);
Whenever I saw the fileobj in log message it is displaying as /abckatte.com/abc/test. One slash is missing.
I tried with appending two more slash like.
String path = "////abckatte.com/abc/test";
then also it is not working.
You could make use of Apache Commons VFS 2, as it provides access to several file systems. Chek it out here, in Local files file://///somehost/someshare/afile.txt.
I would like to know how I can open and display a .txt in a Java application . The .txt is associated with the application and when you click on it , the application opens, but the file does not get to be shown if not by passing a fixed route.
I've got to show it but only if the .txt file is in the same directory as the jar file and run the application only if directly . The direct access from the .txt opens the application but nothing more .
I have this code , you see the path step them directly . I want you to take from the .txt has been clicked .
FileReader f = new FileReader("archivo.txt");
BufferedReader b = new BufferedReader(f);
String linea_cliente = b.readLine();
StringTokenizer datos_cliente = new StringTokenizer(linea_cliente,";");
while(datos_cliente.hasMoreTokens()){
pedido.setText(datos_cliente.nextToken());
id_cliente.setText(datos_cliente.nextToken());
nom_cli.setText(datos_cliente.nextToken());
dir_cli.setText(datos_cliente.nextToken());
cp_cli.setText(datos_cliente.nextToken());
loc_cli.setText(datos_cliente.nextToken());
prov_cli.setText(datos_cliente.nextToken());
pais_cli.setText(datos_cliente.nextToken());
obs_cli.setText(datos_cliente.nextToken());
}
Sorry for my bad English . Thank You ;)
FileReader f = new FileReader("archivo.txt");
Implies that archivo.txt is a relative path. Relative meaning in relation to the current executable. It is an implied .\archivo.txt
You can place it in a sub directory and use a relative path again like .\myfiles\textfiles\archivo.txt where .\ is the location of your jar.
If you want to input many different text files and you don't know where they will be then you can use arguments. From the command line it would look like:
> java jar myproj.jar C:\test\foo\archivo.txt
And to access it in main() use:
String filePath = args[0]
FileReader f = new FileReader(filePath);
If you want it to be portable accross many systems you'll need to take advantage of environment variables to get your base path and then attach the route to your .txt file to the base.
Sorry, it was a little unclear what you were asking for so I covered a few common cases, let me know if you need clarification.
I'm new to java, I got the path from the user, using chooser.getCurrentDirectory(), now i want to use the directory to create a file there, File report = new File(chooser directory + "filename"), but it only accepts string, not file, so how can i get the chooser directory as a string?
You should not use chooser.getCurrentDirectory() to start off with, you should use chooser.getSelectedFile();.
And you should take a look at http://docs.oracle.com/javase/7/docs/api/java/io/File.html, specifically at the get*() methods that involve the filename.
I have some text configuration file that need to be read by my program. My current code is:
protected File getConfigFile() {
URL url = getClass().getResource("wof.txt");
return new File(url.getFile().replaceAll("%20", " "));
}
This works when I run it locally in eclipse, though I did have to do that hack to deal with the space in the path name. The config file is in the same package as the method above. However, when I export the application as a jar I am having problems with it. The jar exists on a shared, mapped network drive Z:. When I run the application from command line I get this error:
java.io.FileNotFoundException: file:\Z:\apps\jar\apps.jar!\vp\fsm\configs\wof.txt
How can I get this working? I just want to tell java to read a file in the same directory as the current class.
Thanks,
Jonah
When the file is inside a jar, you can't use the File class to represent it, since it is a jar: URI. Instead, the URL class itself already gives you with openStream() the possibility to read the contents.
Or you can shortcut this by using getResourceAsStream() instead of getResource().
To get a BufferedReader (which is easier to use, as it has a readLine() method), use the usual stream-wrapping:
InputStream configStream = getClass().getResourceAsStream("wof.txt");
BufferedReader configReader = new BufferedReader(new InputStreamReader(configStream, "UTF-8"));
Instead of "UTF-8" use the encoding actually used by the file (i.e. which you used in the editor).
Another point: Even if you only have file: URIs, you should not do the URL to File-conversion yourself, instead use new File(url.toURI()). This works for other problematic characters as well.