I'm using Java to create a program that takes in a CSV file and outputs an Arff file. Whenever the program runs it comes up catching the exception that No source has been specified. When I delete the try catch it comes with the following error and I am not sure why,
Exception in thread "main" java.io.IOException: No source has been specified
at weka.core.converters.CSVLoader.getDataSet(CSVLoader.java:867)
at CSVtoArff.Convert(CSVtoArff.java:10)
at CSVtoArff.main(CSVtoArff.java:23)
Below is the code for the program
import weka.core.Instances;
import weka.core.converters.CSVLoader;
import weka.core.converters.ArffSaver;
import java.io.File;
public class CSVtoArff {
public static void Convert(String input, String output) throws Exception {
try {
CSVLoader load = new CSVLoader();
load.setSource(new File(input));
Instances data = load.getDataSet();
ArffSaver save = new ArffSaver();
save.setInstances(data);
save.setFile(new File(output));
save.writeBatch();
System.out.println("File successfully converted");
}
catch (Exception e) {
System.out.println("Does not meet arff standards: " + e.getMessage());
}
}
public static void main(String[] args) throws Exception{
String input = "C:\\Users\\jason\\Desktop\\example.csv";
String output =" C:\\Users\\jason\\Desktop\\example.arff";
Convert(input, output);
}
}
Please try putting the files in C:\temp folder and change it to below and try.
Sometime windows security my be denying access to protected system folders.
Also there is an extra leading space in output file path. I have removed that.
public static void main(String[] args) throws Exception{
String input = "C:/temp/example.csv";
String output ="C:/temp/example.arff";
Convert(input, output);
}
Related
When I attempt to run the program that takes the metadata and prints it from an mp3 file, I am returned with an "Exception in thread "main" java.lang.NullPointerException at project.mp3MetaData.main(musicdj.java:18)". For this class you need the jid3lib jar. How do I avoid this exception and do I need to pass any variables through the tags at the bottom?
package 1234;
import java.io.File;
import java.io.IOException;
import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.ID3v1;
public class mp3MetaData {
public static void main(String[] args) throws IOException, TagException {
// TODO Auto-generated method stub
File sourceFile = new File("/Users/JohnSmith/Desktop/MusicTester/1234.mp3");
MP3File mp3file = new MP3File(sourceFile);
ID3v1 tag = mp3file.getID3v1Tag();
System.out.println(tag.getAlbum());
System.out.println(tag.getAlbumTitle());
System.out.println(tag.getTitle());
System.out.println(tag.getComment());
}
}
Any help will be greatly appreciated.
Your MP3 file might not contain an ID3 tag. So check whether tag is null or not, before using it. Something like this:
public static void main(String[] args) throws IOException, TagException
{
File sourceFile = new File("/Users/JohnSmith/Desktop/MusicTester/1234.mp3");
final MP3File mp3file = new MP3File(sourceFile);
final ID3v1 tag = mp3file.getID3v1Tag();
if (null == tag)
{
System.out.println("No ID3 tag found!");
}
else
{
System.out.println(tag.getAlbum());
System.out.println(tag.getAlbumTitle());
System.out.println(tag.getTitle());
System.out.println(tag.getComment());
}
}
I ran the below code and created a file. Where can I find it in my filesystem?
import java.io.*;
public class FileReaderDemo {
public static void main(String args[]) throws Exception {
File f = new File ("wayback.txt");
f.createNewFile();
System.out.println(f.exists());
}
}
Add the following to your program, run it and it'll show you the expected location:
System.out.println(f.getCanonicalFile());
For my project, I am using weka.jar. I am converting a CSV file to ARFF using following code:
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;
import java.io.File;
public class CsvArffConverter
{
public static void Convert(String sourcepath,String destpath) throws Exception
{
// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(sourcepath));
Instances data = loader.getDataSet();
// save ARFF
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
saver.setFile(new File(destpath));
saver.setDestination(new File(destpath));
saver.writeBatch();
}
public static void main(String args[]) throws Exception
{
Convert("C:\\ad\\BSEIT.csv", "C:\\ad\\test.arff");
}
}
However, on executing, I am getting following error:
Cannot create a new output file. Standard out is used.
Exception in thread "main" java.io.IOException: Cannot create a new output file (Reason: java.io.IOException: File already exists.). Standard out is used.
at `enter code here`weka.core.converters.AbstractFileSaver.setDestination(AbstractFileSaver.java:421)
at Predictor.CsvArffConverter.Convert(CsvArffConverter.java:29)
at Predictor.CsvArffConverter.main(CsvArffConverter.java:34)
According to weka mail list, this error is a file issue, may be permission. Other emails suggest to use Java I/O approch to save arff file.
This error is coming from the CSVSaver and indicates that it is unable
to create the directory and/or file that you've specified. More than
likely it is something to do with permissions on where it is trying to
write to.
Try following code.
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;
import java.io.File;
public class CsvArffConverter
{
public static void Convert(String sourcepath,String destpath) throws Exception
{
// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(sourcepath));
Instances dataSet = loader.getDataSet();
// save ARFF
BufferedWriter writer = new BufferedWriter(new FileWriter(destpath));
writer.write(dataSet.toString());
writer.flush();
writer.close();
}
public static void main(String args[]) throws Exception
{
Convert("BSEIT.csv", "test.arff");
}
}
As you can see, I use relative paths. Since absolute path writing may be blocked due to permission issues.
This question already has an answer here:
Why does the execution order between the printStackTrace() and the other methods appear to be nondeterministic?
(1 answer)
Closed 8 years ago.
I was testing some code to read file available in classpath, but i ended up with some interesting thing related to printStackTrace() method of the Throwable class.
here is the code:
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
public class Main {
public static void main(String[] args) throws IOException, URISyntaxException {
try {
System.out.println("before-test1");
test1(); // will throw exception
System.out.println("after-test1");
} catch (Exception e) {
System.out.println("entering catch-1");
e.printStackTrace();
System.out.println("exiting catch-1");
}
try {
System.out.println("before-test2");
test2();
System.out.println("after-test2");
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("before-test3");
test3();
System.out.println("after-test3");
} catch (Exception e) {
e.printStackTrace();
}
}
private static void test1() throws IOException {
String path = "classpath:/test.txt";
File file = new File(path);
String content = FileUtils.readFileToString(file, "UTF-8");
System.out.println("content = " + content);
}
private static void test2() throws IOException {
String path = "/test.txt";
InputStream in = Main.class.getResourceAsStream(path);
String content = IOUtils.toString(in);
System.out.println("content = " + content);
}
private static void test3() throws IOException, URISyntaxException {
String path = "/test.txt";
URL url = Main.class.getResource(path);
File file = new File(url.toURI());
String content = FileUtils.readFileToString(file, "UTF-8");
System.out.println("content = " + content);
}
}
the content of the test.txt is only one line as per below:
anil bharadia
now the expected output of this program is like below:
before-test1
entering catch-1
java.io.FileNotFoundException: classpath:\test.txt (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:874)
at Main.test1(Main.java:50)
at Main.main(Main.java:16)
exiting catch-1
before-test2
content = anil bharadia
after-test2
before-test3
content = anil bharadia
after-test3
and i have got the same output as above when I ran this class for the first time.
Then after I ran the same class again and i have got the following output:
before-test1
entering catch-1
exiting catch-1
before-test2
content = anil bharadia
after-test2
before-test3
java.io.FileNotFoundException: classpath:\test.txt (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:874)
at Main.test1(Main.java:50)
at Main.main(Main.java:16)
content = anil bharadia
after-test3
in above output the stack trace is printed after the execution of the catch block from which its called and also after the test2()'s execution is over.
so that made me think that printStackTrace() method is asynchronous somehow.
i tried to look into the source of the printStackTrace() and found the following code that didn't help me :
public void printStackTrace(PrintStream s) {
synchronized (s) {
s.println(this);
StackTraceElement[] trace = getOurStackTrace();
for (int i=0; i < trace.length; i++)
s.println("\tat " + trace[i]);
Throwable ourCause = getCause();
if (ourCause != null)
ourCause.printStackTraceAsCause(s, trace);
}
}
I tried to google it but not found any explanation.
And when i tried to run the same class again and again, in many cases i have also found the following output :
java.io.FileNotFoundException: classpath:\test.txt (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:874)
at Main.test1(Main.java:50)
at Main.main(Main.java:16)
before-test1
entering catch-1
exiting catch-1
before-test2
content = anil bharadia
after-test2
before-test3
content = anil bharadia
after-test3
this made me think even more , like how the stack trace got printed before calling the test1() method. that should not be possible.
The stacktrace prints go to a different stream - they're printed to the error stream, rather than the standard out stream. This is why sometimes they'll be displayed in a different order.
System.out.println("after-test3"); write the input to out stream, and printStackTrace() method writes the input to System.err stream
public void printStackTrace() {
printStackTrace(System.err);
}
Error goes to System.err stream.
Normal output goes to System.out stream.
And these streams are written by different threads, so you can also expect different sequence in output if you run again.
To get desired output, you need to change as :
e.printStacktrace(System.out);
This will redirect output to System.out stream.
my first java program ..
so I'm trying to create a file and store in my pc using java
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class createfile {
public static void main(String[] args) throws IOException {
int[] numbers = {1,2,3};
for (int item : numbers) {
String key = "file" + item;
File file = File.createTempFile("c:\\",key,".txt");
Writer writer = new OutputStreamWriter(new FileOutputStream(file));
writer.write("abcdefghijklmnopqrstuvwxyz\n");
writer.write("01234567890112345678901234\n");
writer.write("!##$%^&*()-=[]{};':',.<>/?\n");
writer.write("01234567890112345678901234\n");
writer.write("abcdefghijklmnopqrstuvwxyz\n");
writer.close();
}
return file;
}
}
what am I missing here .. I coudln't figured it out. everything seem to follow along the book.
Thanks
===========update ===========
after I took of
- return file ;
- throws IOException ;
- and change to File file = File.createTempFile(key,".txt",new File("c:\\"));
I still get this error
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Unhandled exception type IOException
Unhandled exception type FileNotFoundException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
Unhandled exception type IOException
you have some mistakes in java syntax:
When you declare method as void (here public static void main(....)) it means that method has no return value - so line "return file;" not needed here.
Use use wrong signature (wrong parameters types in File.createTempFile function.
Possible usages are:
createTempFile(String prefix, String suffix)
createTempFile(String prefix, String suffix, File directory)
For additional information about File class use this link: http://docs.oracle.com/javase/6/docs/api/java/io/File.html
Following possible version of working code:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class createfile
{
public static void main(String[] args) throws IOException
{
int[] numbers = {1,2,3};
for (int item : numbers)
{
String key = "file" + item;
File file = File.createTempFile(key,".txt",new File("c:\\"));
Writer writer = new OutputStreamWriter(new FileOutputStream(file));
writer.write("abcdefghijklmnopqrstuvwxyz\n");
writer.write("01234567890112345678901234\n");
writer.write("!##$%^&*()-=[]{};':',.<>/?\n");
writer.write("01234567890112345678901234\n");
writer.write("abcdefghijklmnopqrstuvwxyz\n");
writer.close();
}
}
}
You can also see another sample how to write text to file: http://www.homeandlearn.co.uk/java/write_to_textfile.html. This link use NetBeans as Java Tool for writing code. I strongly suggest to use some IDE (Eclipse,NetBeans) to write code in java.It will mark your compile mistakes and will suggest corrections.
NetBeans site:https://netbeans.org/
Welcome to Java world
public static void main(String[] args) throws IOException { doesn't return anything, so the return file statement is not required
File.createTempFile either takes String, String, File or String, String so File file = File.createTempFile("c:\\", key, ".txt"); won't compile.
Something like, File file = File.createTempFile(key, ".txt", new File("c:\\")); might be a better idea, but is depended on what you want to achieve.
The JavaDocs state that the prefix must be at least three characters long, so you'll need to pad the key value to meet these requirements.
You MAY find using something like...
File file = new File("C:\\" + key + ".txt");
more managable...