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)
Related
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());
}
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());
I am trying to open a text file in java. I am using ubuntu 12.04. Following is my code:
package nlp;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
public class sentence {
public static void main(String[] args) {
System.out.println("hello");
FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
br.close();
}
}
I am using Eclipse for development. It says "FileNotFound". I have put the text file in .class as well as .java folder. Where am I going wrong?
The default execution directory in Eclipse is the root of the project folder. Put the file there or prefix the path with correct underlying folder structure.
you need to put that in try-catch because it throws IOException which is checked Exception.Put the code in try-catch or handle the exception using "public static void main(String[] args) throws IOException"
The file should be in root of the project folder as given below. Your code is working fine.
As Arnaud mentioned try this to find where Java is expecting the file:
System.out.println(new File(".").getAbsolutePath());
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 */
}
}
I'm writing bytes to temp.fls file. After completing the operation, I want to delete the last 256 bytes from the temp.fls file. How can I achieve this? Please help me.
Thanks in advance.
Use RandomAccessFile.setLength() like so:
RandomAccessFile f = new RandomAccessFile(yourFile,"rw");
f.setLength(f.length()-256);
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileDemo {
public static void main(String ... args) throws FileNotFoundException, IOException
{
File file = new File(<FILE_PATH>);
System.err.println("MSR:: RandomAccessFileDemo :: the target length is"+file.length());
RandomAccessFile raf = new RandomAccessFile(file,"rwd");
raf.seek(file.length()-1); // Set the pointer to the end of the file
System.err.println("MSR:: RandomAccessFileDemo :: the file pointer is"+raf.getFilePointer());
raf.setLength(raf.length()-raf.length());
}
}