for FileOutputStream, it will throw a FileNotFoundException if the file doesn't exist, but it will create it if it can.
I dont have a Sample.txt in my project root
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Main {
public static void main(String[] args) {
try {
FileOutputStream s= new FileOutputStream("Sample.txt");
} catch (FileNotFoundException e) {
System.out.println("File not Found");
}
}
}
The problem is:
I cannot see the Output of the "File Not Found" from the Terminal. How did it happen?
Thank you
You can set Sample.txt as a File first and check if it exists with .canWrite()
You still have to put a try/catch around FileOutputStream, but it should never go in the catch block.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class test {
public static void main(String[] args) {
File f = new File("Sample.txt");
if (!f.exists()) {
System.out.println("File not Found");
}
else {
try {
FileOutputStream s = new FileOutputStream(f);
} catch (FileNotFoundException e) {}
}
}
}
Related
I am new to Java programming and I was reading the a file using the BufferedInputStream(). Can someone tell me why I can't read my file? If I print obj.read(), it returns -1 everytime. Instead it should return the unicode value of every character that the stream is reading.
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
public class Main {
public static void main(String[] args) {
try {
FileInputStream obj = new FileInputStream("myfile.txt");
BufferedInputStream obj2= new BufferedInputStream(obj);
while(obj2.read()!=-1)
{
System.out.print((char) obj2.read());
}
}
catch (Exception e)
{
System.out.println(e);
}
System.out.println("program executed");
}
}
But after introducing a local variable the code works, why???
package com.company;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
public class Main {
public static void main(String[] args) {
try {
FileInputStream obj = new FileInputStream("riten.txt");
BufferedInputStream obj2= new BufferedInputStream(obj);
int a;
while((a=obj2.read())!=-1)
{
System.out.print((char)a);
}
}
catch (Exception e)
{
System.out.println(e);
}
System.out.println("program executed");
}
}
You need to read from your BufferedInputStream and not from the FileInputStream. You are mixing things here.
But in addition to your code you need to properly handle streams, i.e. when you open a file you need to close those afterwards.
A fix of your code. I added a local variable to store the read character and then cast it to a character.
public static void main(String[] args) {
try {
FileInputStream obj = new FileInputStream("myfile.txt");
BufferedInputStream obj2= new BufferedInputStream(obj);
int c;
while((c = obj2.read())!=-1)
{
System.out.print((char) c);
}
}
catch (Exception e)
{
System.out.println(e);
}
System.out.println("program executed");
}
While running my java file io program I'm getting FileNotFoundException
I tried changing the directory of the file and most of the other solution mentioned in SO, nothing works.
My code:
package com.HelloWorld;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
FileWriter w=null;
BufferedWriter bw=null;
try {
String s="welcome";
String b="D:\\test.txt";
w=new FileWriter(b);
bw=new BufferedWriter(w);
bw.write(s);
bw.flush();
}
catch(IOException e)
{
System.out.println("exception caught"+e);
}
finally{
try {
if(bw!=null)
bw.close();}
catch(Exception e) {
System.out.println("exception caught"+e);
}
try {
if(w!=null)
{
w.close();
System.out.println("success");
}}
catch(Exception e) {
System.out.println("exception caught"+e);
}
}
}
}
I had already created the file in the D drive so the FileWriter overrides the already created file name because of which it did not write to the file
I use eclipse and put a File called "example.txt" into the bin-folder where the class files are generated (and into the sub-folder of the package). But still, the program allways prints out the error message i wrote for the case the file is not found.
Main Class:
import java.io.BufferedReader;
public class Main {
public static void main(String[] args){
BufferedReader file = Console.file("example.txt");
...
}
Console Class:
public final class Console {
public static BufferedReader file(String args) {
BufferedReader file = null;
try {
file = new BufferedReader(new FileReader(args));
} catch (FileNotFoundException e) {
println("Error, file not found!");
System.exit(1);
}
return file;
}
}
any ideas?
For the Eclipse project, the current path is the project folder, not the BIN directory, you can use the code below to get the current path so that you will know where to put and how to access the file.
import java.io.File;
import java.io.IOException;
public class MainTest {
public static void main(String[] args)
{
File directory = new File("");
try {
System.out.println(directory.getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
So in your case, the path you specify should be: ./bin/example.txt
import java.io.IOException;
public class Test1_Exec {
public static void main(String[] args) throws IOException {
Runtime run = Runtime.getRuntime();
try {
Process p = run.exec("java Test1");
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void main(String[] args)
{
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream("d:\\ppp\\Test1.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("invoked successfully");
}
}
The problem is that if I run the Test1_Exec in the Eclipse, Test1.txt is not created and no error is reported. But if I type "java Test1" in the command window, Test1.txt is created. Test1_Exec.java and Test1.java are in the same src folder; Test1_Exec.class and Test1.class are in the same bin folder. So what's wrong with the Eclipse? My version of Eclipse is Kepler(20130614-0229).
Put bin folder in your classpath
Process p = run.exec("java -cp path/to/bin Test1");
Currently, java is looking for Test1.class inside your project directory.
Don't you need to give the full path for Test1 in the command?
i.e: "java c:\code\Test1" ?
Test1_Exec.java
import java.io.IOException;
public class Test1_Exec {
public static void main(String[] args) throws IOException {
Runtime run = Runtime.getRuntime();
try {
Process p = run.exec("java -cp bin Test1");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Test1.java:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void main(String[] args)
{
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream("d:\\ppp\\Test1.txt");
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Test1_Exec.class and Test1.class are both in the bin folder under JavaTest(project name), and the codes do work. But I want to replace the code "Process p = run.exec("java -cp bin Test1")" with "Process p = run.exec("java Test1")" by adding bin folder( right clikcing JavaTest(project name)->Run As->Run Configuration | Tab Classpath --- User Entries --- Advanced --- Add Folders ), then Test1.txt is not created by new codes. So where is the problem ?
To me program seemed unnecessarily complex. Why not below(if you dont have specific requirement)
import java.io.IOException;
public class Test1_Exec {
public static void main(String[] args) throws IOException {
try {
Test1.createFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void createFile()
{
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream("d:\\ppp\\Test1.txt");
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}