I am connecting XML with odbc:jdbc from java and updating A.xml file.
After the updating is done, I am copying A.xml to different location and renaming that file(lets say copy.xml).
Everything works fine.
But I noticed that if the original A.xml file has been opened during the time of update, the copy.xml file is empty.
Then I decided to write a macro in the A.xml file to auto save the worksheet if any column value changes. This macro works, but
the copy.xml file is still empty.
I am using
List<String> command = new ArrayList<String>();
command.add("cmd.exe");
command.add("/c");
command.add("copy A.xml copy.xml ");
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
to copy file.
Is there a different way to accomplish this.
Here is how you can copy a file using Java IO API:
import static java.nio.file.StandardCopyOption.*;
...
Files.copy(source, target, REPLACE_EXISTING);
Full documentation here
Also, you have to make sure the file is not being updated when you copy it over to the new place.
Related
I've been trying to set up a Scanner to use a File as an input, but it doesn't seem to recognize the filepath. The file exists in the same folder as my .java files.
File errorList = new File("Errors.txt");
Scanner errorIn = new Scanner(errorList);
This results in a FileNotFoundException.
What am I doing wrong, and how can I fix this?
One other approach you could try is, execute the below code in your eclipse (from any of your class), and see where the hello.txt is created, so you get an idea of where Java is looking for the file.
new File("hello.txt").createNewFile();
Then you could either put your Errors.txt in that location or provide the corresponding relative location.
I'm using Hadoop 2.7.1 and coding in Java. I'm able to run a simple mapreduce program where I provide a folder as input to the MapReduce program.
However I want to use a file (full paths are inside ) as input; this file contains all the other files to be processed by the mapper function.
Below is the file content,
/allfiles.txt
- /tmp/aaa/file1.txt
- /tmp/bbb/file2.txt
- /tmp/ccc/file3.txt
How can I specify the input path to MapReduce program as a file , so that it can start processing each file inside ? thanks.
In your driver class, you can read in the file, and add each line as a file for input:
//Read allfiles.txt and put each line into a List (requires at least Java 1.7)
List<String> files = Files.readAllLines(Paths.get("allfiles.txt"), StandardCharsets.UTF_8);
/Loop through the file names and add them as input
for(String file : files) {
//This Path is org.apache.hadoop.fs.Path
FileInputFormat.addInputPath(conf, new Path(file));
}
This is assuming that your allfiles.txt is local to the node on which your MR job is being run, but it's only a small change if allfiles.txt is actually on the HDFS.
I strongly recommended that you check that each file exists on the HDFS before you add it as input.
Instead of creating a file with path to other files, you could use globs.
In your example, you could have defined your inputs as -input /tmp/*/file?.txt
I want to rename a file name xxx.docx to xxx.docx.zip then rename it back to xxx.docx in Java.
Here is my code.
File file = new File(path);
File file2 = new File(path+".zip");
file.renameTo(file2);
File file3 = new File(file.getPath());
file2.renameTo(file3);
It won't work. Thank you.
Edit : The problem is I forgot to close the doc before renaming it.
The code like that works. Most probably some other process has locked the file and made it read only. You have either opened it in word (since it is docx file) or something like that. Maybe it is in a readonly location.
The code is working though. Try with different file and you will see it is fine (I tried it).
I am running a java application (app1) that runs multiple instances of another java application (app2) in parallel using ProcessBuilder and Fixed Thread Pool with the following code:
futures = new ArrayList<Future<String>>();
executor = Executors.newFixedThreadPool(10);
for (int i=0; i<cnt;i++)
{
ProcessBuilder builder = new ProcessBuilder();
builder.command("java","app2"); //I set the class path in the real code
builder.directory(new File("/Users/Evan/Documents/workspace/MyProj/ins_"+i));
builder.inheritIO();
ProcessRunner pr=new ProcessRunner (builder);
futures.add(executor.submit(pr));
}
Each run of app2 has different working directory (the value of i in the folder ins_i is different for each run). The code works, but now in app2, I want to read a file from the working directory of the current process (i.e. /Users/Evan/Documents/workspace/MyProj/ins_"+i). So in app2 code I need a statement that let me know the directory of the current process builder (basically the value of i in "/Users/Evan/Documents/workspace/MyProj/ins_"+i).
I tried this statement:
System.getProperty("user.dir")
and it did not work because it retrieved "/Users/Evan/Documents/workspace/MyProj"
Thank you and I appreciate any help
but now in app2, I want to read a file from the working directory of the current process
If you know the filename you want to read from within the working directory of app2, you can just create a File object, passing the file name of the file you wish to read. The parent folder defaults to the current working directory (which is defined by the ProcessBuilder)
File myFile = new File("FileToRead");
//read from myFile using FileInputStream
I made a small game that requires to play background music on it, I have images, a txt file and an audio file, they all work after exporting the JAR except for the audio file.
here is the code I used to import :
The Images :
(new ImageIcon(getClass().getResource("/data/131868.jpg"))
The Text file :
BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/data/dictionnaire.txt")));
The Audio file ( I included also the code to play it that I found while searching) :
File f =new File(Main.class.getResource("/data/gmu.mp3").getFile());
final Player p=Manager.createRealizedPlayer(f.toURI().toURL());
p.start();
while(true){
if(p.getMediaTime().getSeconds()==p.getDuration().getSeconds()){
p.stop();
p.setMediaTime(new Time(0));
p.start();
}
}
Basically the File Object was : File f = new File("/data/gmu.mp3") I just added modifications to make it look like the others ...
It did work in Eclipse, but not JAR.
You'd better know that: File is just the name of the file, not the file itself. Like the house number, it tells you the house's location, but is doesn't represent the house.
So, you can use it like this:
InputStream is = this.getClass().getResourceAsStream("/data/gmu.mp3");
File fi = new File(is);
Tell me the result:)
Solution
Try placing the gmu.mp3 inside the source before exporting the jar.
Why?
When you export a jar, it wraps all the fun code up that is inside the source folder. File f = new File("/data/gmu.mp3") simply points the program to that file names gmu.mp3 in the file system. If you place the gmu.mp3 inside the source folder and update the File constructor to reflect the new location, the mp3 should get wrapped up into the jar along with all the code.
Let me know how it goes -Scott
A java.io.File represents a jar-ed file as "ThePacked.jar!/path/inside/file.mp3", which makes problem when used as a File.
To read a jar-ed file's content, you can read from an InputStream given by getResourceAsStream(String filename).
or
To use the file as a real java.io.File, (what I did is to) read it from the InputStream and copy it to a location outside the jar (eg. to a temprary file).