So i have a confing file here is an example from that file
<STX><ESC>F16<LF>vartest<ETX>
After reading the config file i need to create a new file but in that new file i should replace the "vartest" with an actual string. What would be the easiest way to do this.
I read the file like this
line17 = scan.nextLine();
Write it like this
bufferW.write(line17);
bufferW.newLine();
JDK11 // IntelliJ IDE
Just use String#replace(), this replaces vartest in your line17 string with mynewstring, ofcourse this can be whatever you want:
line17 = line17.replace("vartest", "mynewstring");
// Alternatively using a variable instead
line17 = line17.replace("vartest", myStringVariable);
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 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");
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.
My current project is using a metadata file to set properties without having to compile. Currently I have it set up in this way:
metadata.txt
[property] value <br/>
[property2] value2
File f = new File("metadata.txt");
BufferedReader in = new BufferedReader(new FileReader(f));
String variable1 = "";
String variable2 = "";
Now read this file using a BufferedReader and getting the information in a certain order. Such as:
variable1 = in.readLine();
variable2 = in.readLine();
I was wondering is there a better way to do this without having to read line by line? I was trying to think of using a loop but I'm not sure how that would work out since I want to set different String variables to each property.
Also I'm not using a GUI in this program so that is the reason why I'm editing the data raw.
Rather use the java.util.Properties API. It's designed exactly for this purpose.
Create a filename.properties file with key=value entries separated by newlines:
key1=value1
key2=value2
key3=value3
Put the file in the classpath (or add its path to the classpath) and load it as follows:
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));
Then you can obtain values by key as follows:
String key1 = properties.getProperty("key1"); // returns value1
See also:
Properties tutorial
I'm not sure this is an answer to this question.
You can use java.util.Properties and its methods to save or load properties from the file. You metadata file looks like a property file, if you don't target doing something special.
The default output of File.toURL() is
file:/c:/foo/bar
These don't appear to work on windows, and need to be changed to
file:///c:/foo/bar
Does the format
file:/foo/bar
work correctly on Unix (I don't have a Unix machine to test on)? Is there a library that can take care of generating a URL from a File that is in the correct format for the current environment?
I've considered using a regex to fix the problem, something like:
fileUrl.replaceFirst("^file:/", "file:///")
However, this isn't quite right, because it will convert a correct URL like:
file:///c:/foo/bar
to:
file://///c:/foo/bar
Update
I'm using Java 1.4 and in this version File.toURL() is not deprecated and both File.toURL().toString() and File.toURI().toString() generate the same (incorrect) URL on windows
The File(String) expects a pathname, not an URL. If you want to construct a File based on a String which actually represents an URL, then you'll need to convert this String back to URL first and make use of File(URI) to construct the File based on URL#toURI().
String urlAsString = "file:/c:/foo/bar";
URL url = new URL(urlAsString);
File file = new File(url.toURI());
Update: since you're on Java 1.4 and URL#toURI() is actually a Java 1.5 method (sorry, overlooked that bit), better use URL#getPath() instead which returns the pathname, so that you can use File(String).
String urlAsString = "file:/c:/foo/bar";
URL url = new URL(urlAsString);
File file = new File(url.getPath());
The File.toURL() method is deprecated - it is recommended that you use the toURI() method instead. If you use that instead, does your problem go away?
Edit:
I understand: you are using Java 4. However, your question did not explain what you were trying to do. If, as you state in the comments, you are attempting to simply read a file, use a FileReader to do so (or a FileInputStream if the file is a binary format).
What do you actually mean with "Does the format file:/c:/foo/bar work correctly on Unix"?
Some examples from Unix.
File file = new File("/tmp/foo.txt"); // this file exists
System.out.println(file.toURI()); // "file:/tmp/foo.txt"
However, you cannot e.g. do this:
File file = new File("file:/tmp/foo.txt");
System.out.println(file.exists()); // false
(If you need a URL instance, do file.toURI().toURL() as the Javadoc says.)
Edit: how about the following, does it help?
URL url = new URL("file:/tmp/foo.txt");
System.out.println(url.getFile()); // "/tmp/foo.txt"
File file = new File(url.getFile());
System.out.println(file.exists()); // true
(Basically very close to BalusC's example which used new File(url.toURI()).)