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());
Related
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);
}
I'm now writing a java class and want to read a txt file in, like this:
public class Myclass {
...
public static void main(String[] args) {
try{
File file = new File(args[0]);
Scanner scanner = new Scanner(file);
int [] array = new int [1000];
int i = 0;
while(scanner.hasNextInt())array[i++] = scanner.nextInt();}
catch (FileNotFoundException exp) {
exp.printStackTrace();}
}
...
}
And for example, use it like java Myclass input.txt. However when I use javac to compile it in Linux, erros throws:
error: cannot find symbol
catch (FileNotFoundException exp) {
^
symbol: class FileNotFoundException
location: class Myclass
That's weird since name of input file hasn't even been passed in. I've tried File file = new File('input.txt'); and it also throws this error, so I don't know what's wrong (System.out.println(new File('input.txt').getAbsolutePath());will print out the correct and existed path).
You need to add correct imports at the begining of a class:
import java.io.FileNotFoundException;
public class Myclass {...
i think you have to compile your class using the following command
javac com/Trail.java
javac <package-name1>/<package-name2>/<classname.java>
then run the following command
java com.Trail test.txt
the you have to ensure test.txt place then it will works for you, let me recommand you the following answer of the question it helps me a lot to run your code here and here
for where you should put your file
note:
try to declare public static void main(String args[]) throws FileNotFoundException
please you have to be inside folder of file which you want to compile
It looks like you did not import the class FileNotFoundException.
Adding a import java.io.FileNotFoundException at the top of your file should resolve the issue.
Assuming all the imports are alright, the next most likely cause is the txt file is not there. You have to put your txt file at the same folder that there are folders like "src", "dist" and "build".
I've figured it out!
declare main like this:
public static void main(String args[]) throws FileNotFoundException{
...
Try to put the location of file that you want to read, something like this:
File file = new File("C:\\text.txt");
Full example:
public static void main(String[] args)
{
File file = new File("C:\\text.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine())
System.out.println(sc.nextLine());
}
The following code cannot find the specific file any tips on how I can't fix that. Any help will be appreciated.
import java.util.Scanner;
import java.io.*;
public class lab8 {
public static void main(String[] args) throws IOException {
FileInputStream fileIn = new FileInputStream("haiku.txt");
Scanner scrn = new Scanner(fileIn);
System.out.println(scrn.nextLine());
}
}
Try using the absolute path of the file by looking at the file properties.
E.g., /home/demouser/haiku.txt (Linux file)
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.
Sorry about readability. Stack appears to be trimming spaces from code lines & indents don't show up. Hrmph.
This was printing to the console without any problems...
CGT\whgdata\whnvp33.txt << EXPECTED OUTPUT (excerpt)
CGT\whgdata\whnvt30.txt
CGT\whgdata\whnvt31.txt
CGT\whgdata\whnvt32.txt
CGT\whgdata\whnvt33.txt
CGT\whgdef.txt
CGT\whgdhtml.txt
CGT\whibody.txt
etc....
...until I tried printing the hashtable to a file. Since that point, getFileListing isn't recognized as a valid symbol.
FileListing2.java:17: error: cannot find symbol
List<File> files = FileListing2.getFileListing(startingDirectory);
symbol: method getFileListing(File)
location: class FileListing2
1 error
Can someone lend a second set of eyes to help me uncover what I accidentally/overwrote. I'm sure it's something obvious. :\
import java.util.*;
import java.io.*;
import java.nio.*;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption.*;
import java.nio.file.Paths;
//
public final class FileListing2 {
public static void main(String... aArgs) {
//
File startingDirectory= new File("CGT");
File outputFile = new File("CGTOutput.txt");
List<File> files = FileListing2.getFileListing(startingDirectory);
OutputStream output = null;
//
for(File file : files ) {
System.out.println(file); //print filenames
}
}
}
If your code is all you have for FileListing2, than there is no getFileListing() method for LileListing2, only a main() method
Yeah it IS something very obious, your class FileListing2 does not contain a method getFileListing(File). And it has to be static, the way you're trying to call it:
public final class FileListing2 {
public static void main(String... aArgs) {
//
File startingDirectory= new File("CGT");
File outputFile = new File("CGTOutput.txt");
List<File> files = FileListing2.getFileListing(startingDirectory);
OutputStream output = null;
//
for(File file : files ) {
System.out.println(file); //print filenames
}
}
public static List<File> getFileListing(File f) {
/* implementation */
}
}