Couldn't open mallet logging.properties file - java

I try to run ParallelTopicModel class from mallet, i'm using NetBeans to compile it, but when i run the code i get this error statement:
Couldn't open cc.mallet.util.MalletLogger resources/logging.properties file.
Perhaps the 'resources' directories weren't copied into the 'class' directory.
I haven't change any code anyway, still use the original from the class:
public static void main (String[] args) {
try {
InstanceList training = InstanceList.load (new File(args[0]));
int numTopics = args.length > 1 ? Integer.parseInt(args[1]) : 200;
ParallelTopicModel lda = new ParallelTopicModel (numTopics, 50.0, 0.01);
lda.printLogLikelihood = true;
lda.setTopicDisplay(50, 7);
lda.addInstances(training);
lda.setNumThreads(Integer.parseInt(args[2]));
lda.estimate();
logger.info("printing state");
lda.printState(new File("state.gz"));
logger.info("finished printing");
} catch (Exception e) {
e.printStackTrace();
}
}
I am very new to mallet, so I don't know what's that mean, and how can I fix it? Any help would be appreciated.

Mallet is looking for a Java property java.util.logging.config.file. If it doesn't find it, it looks for the resources/logging.properties file, and if it doesn't find that it throws the error you saw.
The default Mallet logging file is at https://github.com/mimno/Mallet/blob/master/src/cc/mallet/util/resources/logging.properties.
You'll need to consult NetBeans documentation for how to set the Java property.

Related

FileNotFoundException coming from Java web application

I am working on the web application with Eclipse. I have created one property file for database configuration. (DBProperty.properties)
Please find below screen-shot of the folder structure.
I want to access this property file. I am accessing with below code.
FileInputStream input = new FileInputStream("src/resources/DBProperty.properties");
I have also tried many relative paths but not able to succeed.
I have set build path for this project.
You need to use
MyClass.class.getClassLoader().getResourceAsStream("DBProperty.properties")
FileInputStream input = new FileInputStream("resources/DBProperty.properties");
Please try the above line of code. Hope it will solve your problem.
The src directory isn't there at runtime.
Resources are not files.
You need to look into Class.getResource() and friends.
You have to specify complete file path with File object.
public static void main(String[] args) {
File file = new File("C:\\Path\\workspace\\jbossmqimpl\\Test1\\resources\\NewFile.xml");
try (FileInputStream fis = new FileInputStream(file)) {
System.out.println("Total file size to read (in bytes) : "+ fis.available());
int content;
while ((content = fis.read()) != -1) {
// convert to char and display it
System.out.print((char) content);
}
} catch (IOException e) {
e.printStackTrace();
}
}

Directory not showing up in desktop, and file not being created?

The following program has the purpose of creating a directory,
folderforallofmyjavafiles.mkdir();
and making a file to go inside that directory,
File myfile = new File("C:\\Users\\username\\Desktop\\folderforallofmyjavafiles\\test.txt");
There are two problems though. One is that it says the directory is being created at the desktop, but when checking for the directory, it is not there. Also, when creating the file, I get the exception
ERROR: java.io.FileNotFoundException: folderforallofmyjavafiles\test.txt (The system cannot find the path specified)
Please help me resolve these issues, here is the full code:
package mypackage;
import java.io.*;
public class Createwriteaddopenread {
public static void main(String[] args) {
File folderforallofmyjavafiles = new File("C:\\Users\\username\\Desktop");
try {
folderforallofmyjavafiles.mkdir(); //Creates a directory (mkdirs makes a directory)
if (folderforallofmyjavafiles.isDirectory() == true) {
System.out.println("Folder created at " + "'" + folderforallofmyjavafiles.getPath() + "'");
}
} catch (Exception e) {
System.out.println("Not working...?");
}
File myfile = new File("C:\\Users\\username\\Desktop\\folderforallofmyjavafiles\\test.txt");
//I even tried this:
//File myfile = new File("folderforallofmyjavafiles/test.txt");
//write your name and age through the file
try {
PrintWriter output = new PrintWriter(myfile); //Going to write to myfile
//This may throw an exception, so I always need a try catch when writing to a file
output.println("myname");
output.println("myage");
output.close();
System.out.println("File created");
} catch (IOException e) {
System.out.printf("ERROR: %s\n", e); //e is the IOException
}
}
}
Thank you so much for helping me out, I really appreciate it.
:)
You're creating the Desktop folder in the C:\Users\username folder. If you check the return value of mkdir, you'd notice it's false because the folder already exists.
How would the system know that you want a folder named folderforallofmyjavafiles unless you tell it so?
So, you didn't create the folder, and then you try to create a file in the (nonexistent) folder, and Java tells you the folder doesn't exist.
Agreed that it's a bit obscure, using a FileNotFoundException, but the text does say "The system cannot find the path specified".
Update
You're probably confused about the variable name, so let me say this. The following are all the same:
File folderforallofmyjavafiles = new File("C:\\Users\\username\\Desktop");
folderforallofmyjavafiles.mkdir();
File x = new File("C:\\Users\\username\\Desktop");
x.mkdir();
File folderToCreate = new File("C:\\Users\\username\\Desktop");
folderToCreate.mkdir();
File gobbledygook = new File("C:\\Users\\username\\Desktop");
gobbledygook.mkdir();
new File("C:\\Users\\username\\Desktop").mkdir();

FileNotFoundException - Reading a text file in java

I'm getting a file not found exception from this code even though it's within the try catch statement and I'm not sure what's wrong, the file is within the project folder and is called 'someFile.txt'. This is the main method:
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("no arguments given");
return;
}
double FRE = sortFile(args[0]);
System.out.println("Readability of file " + args[0] + "= " + FRE);
}
And this is the sortFile method where the exception occurs:
public static double sortFile(String FileName) {
int nWords = 0;
int nSyllables = 0;
int nSentences = 0;
File text = new File(FileName);
try {
Scanner sc = new Scanner(text);
while (sc.hasNext()) {
contents.add(sc.next());
++nWords;
}
sc.close();
for (String e : contents) {
getNumSyllables(e);
}
} catch (FileNotFoundException e) {
System.out.println("The file" + FileName + "could not be opened.");
e.printStackTrace();
}
double FRE = getFRE(nWords, nSyllables, nSentences);
return FRE;
}
Thanks for any help :)
well, the file does not exist in that location. Try to add
System.out.println(text.getAbsolutePath())
to see where the file is expected. Note, when you provide a relative path (e.g. some/path/filename.ext), this is relative to the working directory. The working directory is the folder your java program is started in.
If you're using an IDE (e.g. Eclipse, IntelliJ, Netbeans) you can define the working directory in your run configuration.
See:
Javadoc of java.io.File to learn how relative paths work inside a Java environment: http://docs.oracle.com/javase/7/docs/api/java/io/File.html
working dir: Getting the Current Working Directory in Java
I'm getting a file not found exception from this code even though it's
within the try catch statement
The try-catch does not prevent the Exception from being thrown. It merely executes the code in the catch block when an Exception is thrown, and you are just printing the stack trace in the catch block, which is what usually printed anyways on uncaught exceptions.
To resolve your actual issue, first try passing the full path to the file, verify that it works and then use Tim's answer to debug your absolute path.
Try launching your program with the absolute path.
java yourclassname absolutepath_to_someFile.txt

How to move specific files from one folder to another using java

I have tried and succeed moving files from one folder to another folder using java . Here is my code
File source = new File("D:\\polo\\");
File desc = new File("E:\\polo2\\");
try {
FileUtils.copyDirectory(source, desc);
} catch (IOException e) {
e.printStackTrace();
}
But i would like to move specific files from one folder to the other not all the files. Is this possible to do in java. Please help us on this
You can use Java SE standard utility
java.nio.file.Files.copy(Path source, Path target, CopyOption... options)
use renameTo
public static void main(String[] args)
{
try{
File source = new File("D:\\polo\\");
File desc = new File("E:\\polo2\\");
if(source .renameTo(new File("E:\\polo2\\" + afile.getName()))){
System.out.println("File is moved successful!");
}else{
System.out.println("File is failed to move!");
}
}catch(Exception e){
e.printStackTrace();
}
}
In java 1.7, new IO classes were added, including the Files utility class which has a method copy.
There is an example of usage here.
Use IOUtils Library for copy file from one location to another location.
For eg.
File source = new File("D:\\polo\\fileold");
File desc = new File("E:\\polo2\\filenew");
IOUtils.copy(source, desc);
Try this..

Java FileNotFound Exception even when using absolute path

There are a lot of topics on this problem, but not one seems to have the answer I am looking for. I am attempting to open a file for read/write, but I get the file not found exception. I specified the absolute path, but to no avail. When I check "exists" and "canread" both return false. I have tried multiple files, and the result is always false. Someone mentioned it could be a permission issue, but I don't know how to fix that. Once more, if "exists" returns false, I doubt its just permission issues. Any help would be appreciated.
File myfile = new File("C:\\Users\\Eric\\workspace\\ReadJPG\\test.txt");
//File myfile = new File("C:/Users/Eric/workspace/ReadJPG/test.txt");
boolean h = myfile.canRead();
boolean p = myfile.exists();
try {
FileInputStream fis = new FileInputStream(myfile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Check the path. The format you're using works. I tried to replicate, but could only by miswriting the file name. My code:
import java.io.File;
public class Main {
public static void main(String[] args) {
File myfile = new File("C:\\Users\\iajrz\\Desktop\\usepass.txt");
System.out.println(myfile.exists());
}
}
prints true. Even if you had permission issues, "exists()" should return true if the file exists. Permissions wouldn't let you read, or write; they wouldn't forbid you from knowing that the file exists (i.e. listing). I tried.

Categories