pass parameter "a" to class java for the command line argument - java

public static void main(String[] args)
{
for (String a : args) {
System.out.println(a);
}
public class CustomConfiguration {
public static void readConfig(String filename) {
//read from config.properties file
try {
String File = filename;
System.out.println ("ConfigFile :" + File);
String result = "";
Properties properties = new Properties();
String propFileName = filename;
InputStream inputStream = new FileInputStream(propFileName);
}
}
My question is how can i pass the "a" to the CustomConfiguration class?

As other users have pointed that out already - using propertiesFile property can make your application really flexible as you only need to change the propertiesFile content and your application will obtain the necessary information from it. For example, if you had the following in your properties file:
myFileName=C:/gherkins
typeOfMeat=Beef
typeOfRisk=Very High
you can extract the myFileName property and get the value `C:/gherkins' in your class wherever you want. In case you change the values in your properties file, your class doesn't have to do anything except getting the new information - easy. If you are unclear, follow this - http://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/
Alternatively:
You can use String[] args in main method to pass the filename you are interested in. You can use that everywhere else. args[0] is always the path of the class file you are running and args[1], args[2], etc. will be the extra arguments supplied to the class application. In your case you want to extract the file name by doing String myFileName = args[1]; and use myFileName in other places.
p.s. in your code the filename path is C://... You sure this // is correct? you might wanted to put C:\\my_file_name escaping backslash? or just use one forward slash?

Related

Copying the content of a list into a folder

I have an arrayList "rules" containing Rules. Each Rule is an XML file and have some properties such as filename...
I want to copy the Rules from the arraylist to a folder named AllMRG. I tried the code between comments but I get the message "Source 'RG6.31.xml' does not exist".
I changed the code by the following, but there is still a problem with 'RG6.31.xml' and the folder AllMRG is empty even though the arrayList contains many Rules!
First attemption:
File AllMRGFolder = new File("AllMRG");
for(int p = 0; p < rules.size(); p++) {
/* File MRGFile = new File(rules.get(p).fileName);
FileUtils.copyFileToDirectory(MRGFile, AllMRGFolder); */
File MRGFile = new File("AllMRG/" + rules.get(p).fileName);
if (!MRGFile.exists()) {
FileUtils.copyFileToDirectory(MRGFile, AllMRGFolder);
}
}
Second attemption:
String path = "AllMRG";
for(Rule rule : rules) {
File MRGFile = new File(rule.fileName);
Files.copy(MRGFile.toPath(), (new File(path + MRGFile.getName())).toPath(), StandardCopyOption.REPLACE_EXISTING);
}
PS: Rule is a class
public class Rule implements Comparable{
public String fileName;
public String matches;
public String TPinstances;
public int nbrOfMatches;
public double T;
#Override
public int compareTo(Object o) {
if(o instanceof Rule){
//processing to compare one Rule with another
}
return 0;
}
}
Here is the entire code after having considered Shyam's answer. The same problem persists!
Path directoryPath = Files.createDirectory(Paths.get("AllMGR"));
for(Rule rule : rules) {
Path filePath = directoryPath.resolve(rule.fileName);
Files.createFile(filePath);
File MRGFile = new File(rule.fileName);
String ruleContent = new String(Files.readAllBytes(Paths.get(MRGFile.getPath())));
String fileContent = new String(Files.readAllBytes(filePath));
fileContent=ruleContent;
PrintWriter out13= new PrintWriter("AllMGR/"+rule.fileName+".xml");
out13.print(fileContent);
out13.close();
}
Firstly, you are creating a new File with rule.filename without giving any predefined path. Then, you are building a path like: path + MRGFile.getName() without any path delimiters and trying to copy the file to this location. I don't think this will work.
What can actually help you is, creating a base directory first and then creating individual files in it.
Create base directory:
Path directoryPath = Files.createDirectory(Paths.get("AllMGRDir"));
Then for each of your Rule object you can crate file using:
for(Rule rule : rules) {
Path filePath = directoryPath.resolve(rule.fileName());
Files.createFile(filePath);
// your remaining code
}
The resolve(String other) method resolves the given path. Java doc says that:
Converts a given path string to a Path and resolves it against this
Path in exactly the manner specified by the resolve(Path) method.
For example, suppose that the name separator is "/" and a path
represents "foo/bar", then invoking this method with the path string
"gus" will result in the Path "foo/bar/gus"
Hope this helps.

Java Regex Expression isn't working for File Validation

I'm trying to validate a filepath a user has entered through a swing box. They click on the location where they want the file and then they add the name of the file by themselves. So they need to make sure it is in the format such as "C:/files/documents/hello.txt and they need to specify the file type at the end so i can create a new file to write to. The isFile method doesn't seem to satisfy this as the file has to exist, so i'm trying to use regex now with an if statement to validate the file path .
public class Main {
public static void main (String [] args) throws FileNotFoundException {
String fileName = "C:/users/furquan/hello.txt";
File zerina = new File (fileName);
//FileInputStream fis = new FileInputStream (zerina);
String regex = "\\^(?:[\w]\:|\\\)(\\\[a-z_\-\s0-9\.]+)+\\\.(txt|gif|pdf|doc|docx|xls|xlsx)$";
System.out.println (fileName.matches(regex));
}
}
I know you need to add more slashes in java regex because of the escape sequence but i can't get it to work
Use java.nio.file.Path. (you don't need Regexs)
for example :
// imports
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Path path = Paths.get("C:/users/furquan/hello.txt");
Path parent = path.getParent();
if (Files.exists(parent) && path.getFileName().endsWith(".txt")) {
// your code goes here
}
EDIT :
Also, if you want to validate file names :
static final Pattern FILE_NAME_PATTERN = Pattern.compile("([\\w\\d\\s\\-\\.])+\\.(txt|gif|pdf|doc|docx|xls|xlsx)");
public static void main(String[] args) throws Exception {
String s = "C:/users/furquan/hello.txt";
System.out.println(createFile(s));
}
static boolean createFile(String fileName) throws IOException {
Path path = Paths.get(fileName);
if (Files.exists(path)) {
return true;
}
if (FILE_NAME_PATTERN.matcher(path.getFileName().toString()).matches()) {
Path parent = path.getParent();
if (!Files.exists(parent)) {
Files.createDirectories(parent);
}
Files.createFile(path);
return true;
}
return false;
}
You have way too much escaping and unnecessary stuff in your regex. None of the characters you're dealing with need escaping, and in fact escaping them breaks your regex.
Try this:
String regex = "(?i)[A-Z]:[\\\\\\w\\s.-]+)\\.(txt|gif|pdf|docx|doc|xlsx|xls)";
Since matches() must match the whole string, ^ and $ are implied, so leave them out.

How to change the output file name from part-00000 in reducer to inputfile name

Currently I am able to implement the name change from part-00000 to a custom fileName in mapper. I am doing this by taking the inputSplit. I tried the same in reducer to rename the file but, fileSplit method is not available for reducer. So, is there a best way to rename the output of a reducer to with inputfile name. Below is how I acheived it in mapper.
#Override
public void setup(Context con) throws IOException, InterruptedException {
fileName = ((FileSplit) con.getInputSplit()).getPath().getName();
fileName = fileName.substring(0,36);
outputName = new Text(fileName);
final Path baseOutputPath = FileOutputFormat.getOutputPath(con);
final Path outputFilePath = new Path(baseOutputPath, fileName);
TextOutputFormat<IntWritable, Text> write = new TextOutputFormat<IntWritable, Text>() {
#Override
public Path getDefaultWorkFile(TaskAttemptContext context, String extension) throws IOException {
return outputFilePath;
This is what hadoop wiki says:
You can subclass the OutputFormat.java class and write your own. You can locate and browse the code of TextOutputFormat, MultipleOutputFormat.java, etc. for reference. It might be the case that you only need to do minor changes to any of the existing Output Format classes. To do that you can just subclass that class and override the methods you need to change.
If you need to be on key and input file format, then you could create subclass of MultipleOutputFormat to control output file name.

Text input and output java

I am trying to read 2 files after i read the files i want to get their contents and manipulate the contents of the two files then update a new file which is the output. The files are in the same folder as the program but the program always throws a FileNotFoundException.
Below is my code:-
import java.io.*;
import java.util.Scanner;
public class UpdateMaster {
public static void main(String[] args)
{
String master = "Customer.dat";
String trans = "Transactns.dat";
String newMaster = "Temp.txt";
Scanner inputStreamMaster = null;
Scanner inputStreamTrans = null;
PrintWriter inputStreamNewMaster = null;
try
{
inputStreamMaster = new Scanner(new File(master));
inputStreamTrans = new Scanner(new File(trans));
inputStreamNewMaster = new PrintWriter(newMaster);
}
catch(FileNotFoundException e)
{
System.out.println("Error: you opend a file that does not exist.");
System.exit(0);
}
catch(IOException e)
{
System.out.println("Error.");
System.exit(0);
}
do
{
String transLine = inputStreamTrans.nextLine();
String masterLine = inputStreamMaster.nextLine();
String[] transLineArr = transLine.split(",");
String[] masterLineArr = masterLine.split(",");
int trAccNo = Integer.parseInt(transLineArr[0]);
int sales = Integer.parseInt(transLineArr[1]);
int masterAccNo = Integer.parseInt(masterLineArr[0]);
int balance = Integer.parseInt(masterLineArr[1]);
while(masterAccNo== trAccNo){
inputStreamNewMaster.println(trAccNo+ " , "+masterAccNo);
masterLine = inputStreamMaster.nextLine();
masterLineArr = masterLine.split(",");
masterAccNo = Integer.parseInt(masterLineArr[0]);
balance = Integer.parseInt(masterLineArr[1]);
}
balance = balance + sales;
inputStreamNewMaster.println(masterAccNo+ " , "+balance);
}while(inputStreamTrans.hasNextLine());
inputStreamMaster.close();
inputStreamTrans.close();
inputStreamNewMaster.close();
//System.out.println(" the line were written to "+ newMaster);
}
}
Like #Ankit Rustagi said in the comments, you need the full path of the files if you want to keep the current implementation.
However, there is a solution where you only need the file names: use BufferedReader / BufferedWriter. See here an example on how to use these classes (in the example it uses the full path but it works without it too).
Use absolute path
String master = "C:/Data/Customer.dat";
String trans = "C:/Data/Transactns.dat";
String newMaster = "C:/Data/Temp.txt";
The code works for me, i guess you misspelled some filename(s) or your files are in the wrong folder. I created your files on the same level as the src or the project. Also this is the folder where the files are exspected.
There's nothing wrong with using relative paths like tihis. What's happening is that your program is looking for the files in the directory where you execute the program, which doesn't have to be the folder of the program. You can confirm this by logging the absolute path of the files before you try to read them. For example:
File masterFile = new File(master);
System.out.printf("Using master file '%s'%n", masterFile.getAbsolutePath());
inputStreamMaster = new Scanner(masterFile);
In general you should not hardcode file paths but allow the user to specify them in someway, for example using command line arguments, a configuration file with a well known path, or an interactive user interface.
There is a way to locate the program's class file but it's a little tricky because Java allows classes to be loaded from compressed archives that may be located in remote systems. It's better to solve this problem in some other manner.
Try this:
String current = new java.io.File( "." ).getCanonicalPath();
System.out.println("I look for files in:"+current);
To see what directory your program expects to find its input files. If it shows the correct directory, check spelling of filenames. Otherwise, you have a clue as to what's gone wrong.

Java String as Part if a Path

I would like to have a directory path that is A/%Name%/B, where %Name% is a string I declared earlier, is there a Path.Combine like in C#? Or what could I use?
If I understand it correctly , you are trying to format a String.
You can use
String directoryName = "test";
String path = "A/%s/B";
String.format(path,directory);
or something like below based on your requirement
File f = new File(String.format(path,directory));
You can use:
String yourString = ...;
File theFile = new File("A/" + yourString + "/B");
Use the File constructor:
File combined = new File(new File("A", name), "B");
You could even write a convenience method to do that if you wanted:
public static File combine(String base, String... sections)
{
File file = new File(base);
for (String section : sections) {
file = new File(file, section);
}
return file;
}
Then you can call it as:
File x = combine("A", name, "B");
Note that using the File constructor like this is generally considered preferable to assuming a directory separator of /, even though in practice that works on all platforms that I'm aware of.

Categories

Resources