Unix: "ls" command shows files with ? after the extension - java

I have written a shell script (test.sh) using Java. This shell script actually does a copy job from one file to the other. After the execution of the shell script I have opened the directory from Console and typed ls. It shows the output file with ? after the extension.
example : foo.csv?
File execFile = new File(file);
FileWriter fwFile;
try {
fwFile = new FileWriter(execFile);
execFile.setExecutable(true);
BufferedWriter bwFile = new BufferedWriter(fwFile);
bwFile.write(strOutput.substring(0, 2));
bwFile.write("\r\n");
bwFile.write("cd " + strOutput);
bwFile.write("\r\n");
bwFile.write("mkdir " + strOutput);
bwFile.write("\r\n");
bwFile.write(strUnixPath);
bwFile.write("\r\n");
bwFile.write("cd " + strWorkingPath + IPlatinumConstants.FS+"lib"+IPlatinumConstants.FS+"Unx");
bwFile.write("\r\n");
bwFile.write("echo Cut Src Start time %time%");
bwFile.write("\r\n");
bwFile.write("cp " + " \"" + strSourceFilePath + "\" \""
+ strOutput + "copy_A\"");
bwFile.write("\r\n");
My guess is that, while creating the shell script using java, something needs to taken care of

bwFile.write("\r\n");
replace these lines with UNIX line endings
bwFile.write("\n");
or set the proper line separator
System.setProperty("line.separator", "\n");
bwFile.newLine()

Related

Alternative ways to rename a flat file in Java

So im writing a program that reads a txt file that the user provides the name of from a specific folder
I want to rename the .txt after the user has opened it. However, nothing happens
temp = userInput;
currentFileDir = "D:\\Document\\" + username + "\\" + temp1 ;
File directory = new File(currentFileDir + ".txt");
Scanner readingFile = new Scanner(directory);
while (readingFile.hasNextLine())
{
txtdata = txtdata + readingFile.nextLine() + " ";
}
File newName = new File(currentFileDir + "--OPENED.txt");
directory.renameTo(newName);
System.out.println(txtdata);
}
Is there something wrong with the code I provided?
I've tried using it in a standalone program and it works fine, so I think that the rest of the code of my program must interfere with the rename process.
Are there any alternatives to "renameTo"?

How to pass arguments to pre compiled java code

I need to process a high volume of resumes. And want to use this parser:
https://github.com/antonydeepak/ResumeParser
But you run it in powershell with the file to read and the output file.
But I do not know how to automate this, so it read a whole folder containing the resumes.
I know some Java, but cant open the code. Is scripinting in powershell the way to go?
Thanks!
> java -cp '.\bin\*;..\GATEFiles\lib\*;..\GATEFILES\bin\gate.jar;.\lib\*'
code4goal.antony.resumeparser.ResumeParserProgram <input_file> [output_file]
Either make a batch file from an edited directory listing, or write a program.
As this is stackoverflow:
So starting with the same classpath (-cp ...) you can run your own program
public void static main(String[] args) throws IOException {
File[] files = new File("C:/resumes").listFiles();
File outputDir = new File("C:/results");
outputDir.mkDirs();
if (files != null) {
for (File file : files) {
String path = file.getPath();
if (path.endsWith(".pdf")) {
String output = new File(outputDir,
file.getName().replaceFirst("\\.\\w+$", "") + ".json").getPath();
String[] params = {path, output);
ResumeParserProgram.main(params);
// For creating a batch file >x.bat
System.out.println("java -cp"
+ " '.\\bin\\*;..\\GATEFiles\lib\\*;"
+ "..\\GATEFILES\\bin\\gate.jar;.\\lib\\*'"
+ " code4goal.antony.resumeparser.ResumeParserProgram"
+ " \"" + path + "\" \"" + output + "\"");
}
}
}
}
Check that this works, that ResumeParserProgram.main is reenterable.

Importing .sql file into MySQL using Java code

I'm trying to programmatically import a .sql file into MySQL. The .sql file was generated by mysqldump. I'm trying to do this dynamically in a Java program. But, it keeps failing on the "<" character within Java (I think). If I grab the String in the debugger of the command it's about to run (the "combined" variable below), and paste it into the command line, it works fine. Likewise, when I was trying to get the mysqldump working inside this program, it failed on the ">" character, and I had to replace it with the "--result-file=" argument to get it to work.
String command = mySqlPath + "mysql.exe";
String user = "-u " + settings.dbUser;
String password = "-p" + settings.dbPassword;
String db = settings.dbDatabase;
String inputFile = filePath + mySqlDumpFile;
String combined = command + " " + user + " " + password + " " + db + " < " + inputFile;
ExternalCommandExecuter ece = new ExternalCommandExecuter(combined);
int code = ece.execute();
This results in this String for example
C:\software\mysql5\bin\mysql.exe -u root -p<password>
db_name < C:\software\tomcat7\webapps\ROOT\WEB-INF\documents\dump-1461789460425.sql
Which will result in a exitCode of 1. Pasting it into the command line, and it'll work.
So I couldn't get it to work in Java by importing the file with a "<", instead I had to write an external .sh/.bat file which I call from my Java code.

creating a silent wav file with ffmpeg

i want to know how to create ffmpeg file that is silent for a certain length that is dependant on a variable called DIFF, this is what i got so far that doesn't seem to work
// creates a wav file for the lenth of the silence
File silence = new File(root + "silence.wav");
String lthacommand = "ffmpeg -filter_complex aevalsrc=0 -t " + diff
+ " " + silence.getAbsolutePath();
new ThreadControl().executeCommand(lthacommand);

Convert image using image magick via java command says error

I need to convert all the tif, jpeg, gif to jpg format. For this am using
ProcessBuilder pb2 = new ProcessBuilder("convert.exe", "\"" +dest.toString()+ "\" ", "\" " + dest.getParent().toString().concat("/").concat(dest.getName().toString().substring(0, dest.getName().toString().lastIndexOf(".")).concat(".jpg"))+ "\" " );
System.out.println("convert " + "\"" + dest.toString() + "\" " + "\" " + dest.getParent().toString().concat("/").concat(dest.getName().toString().substring(0, dest.getName().toString().lastIndexOf(".")).concat(".jpg")) + "\" " );
pb2.redirectErrorStream(true);
try {
Process p2 = pb2.start();
System.out.println("jpg done for " + dest.getName());
new Thread(new InputConsumer(p2.getInputStream())).start();
try {
System.out.println("Exited with: " + p2.waitFor());
} catch (InterruptedException ex) {
Logger.getLogger(ImageFileCopy.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(ImageFileCopy.class.getName()).log(Level.SEVERE, null, ex);
}
It is saying error
"Invalid parameter - and
Exited with: 4"
I also tried giving "C:\Program Files\ImageMagick-6.8.6-Q16\convert.exe". If i use full path system not showing error but wating for long time.
Any idea plz suggest.
If you're using ProcessBuilder there's no need to "quote" your parameters, this is the point of using ProcessBuilder, it will guarantee that each separate parameter is passed as an argument to the command
ProcessBuilder pb2 = new ProcessBuilder(
"convert.exe",
dest.toString(),
dest.getParent().toString().concat("/").concat(dest.getName().toString().substring(0, dest.getName().toString().lastIndexOf(".")).concat(".jpg")));
I also agree with Rafael's suggestion, a wrapper API will make life a LOT easier ...
[face palm]...Windows has it's own convert program which is accessible via the PATH environment variable.
Even when I used pb.directory and set the directory to the install location of ImageMagick, it still picked up the Windows/MS program...
Try adding the full path to convert.exe
ProcessBuilder pb2 = new ProcessBuilder(
"C:\\Program Files\\ImageMagick-6.8.6-Q16\\convert.exconvert.exe",
dest.toString(),
dest.getParent().toString().concat("/").concat(dest.getName().toString().substring(0, dest.getName().toString().lastIndexOf(".")).concat(".jpg")));
And thanks to this answer for pointing it out...
I recommend to use im4java to call ImageMagick from your java code.
It is opensource, has API to call many ImageMagick functions and is easy to use.
invokation of an ImageMagick resize-function (for example) looks like that:
// create command
ConvertCmd cmd = new ConvertCmd();
// create the operation, add images and operators/options
IMOperation op = new IMOperation();
op.addImage("myimage.jpg");
op.resize(800,600);
op.addImage("myimage_small.jpg");
// execute the operation
cmd.run(op);
Check this simple developer's guide for more information.

Categories

Resources