-3

I have the below piece of code.

import java.io.*;
public class FileTest {
    public static void main(String[] args) throws IOException {
        WriteLinesToFile("miss.txt","This is a special file");
    }

    public static void WriteLinesToFile(String outputFileName, String lineConverted) throws IOException {
        File f = new File(outputFileName);
        if (f.createNewFile()) {
            System.out.println("File is created!");
            FileWriter writer = new FileWriter(f);
            writer.write(lineConverted);
            writer.close();
        } else {
            System.out.println("File already exists.");
            FileWriter writer = new FileWriter(f);
            writer.write(lineConverted);
            writer.close();
        }
    }
}

I need the same logic, without throwing exception. Could someone tell me how to do this?

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 3
    So you have a piece of code that throws an exception because something is faulty? You should probably... fix the problem instead of trying to not throw the exception. Also please note that methods by convention start with a non-capitalized letter, so it should be `writeLinesToFile`. – Ben Jul 06 '18 at 08:00
  • You just need to surround your code with try-catch block. – Tiago Mussi Jul 06 '18 at 08:00
  • 4
    What do you mean "without throwing exception"? You mean you want to avoid the `throws` clause in the methods? – Kayaman Jul 06 '18 at 08:00
  • 2
    What happened with your indentation? – khelwood Jul 06 '18 at 08:02
  • 1
    Does your code throw an exception? Or as mentioned by @Kayaman, you're not happy with adding `throws IOException` in the method declaration? If the latter, read about [checked exceptions](https://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation) – achAmháin Jul 06 '18 at 08:03
  • 2
    If the file cannot be written for various reasons, what would you want to happen instead of an exception being thrown? I don’t think there’s any way of writing to a file without a little risk of an exception, BTW. – Ole V.V. Jul 06 '18 at 08:05
  • catch the expected exceptions inside `writeLinesToFile` and they won't be thrown to the callers of `writeLinesToFile` – Adrian W Jul 06 '18 at 08:10
  • Some suggestions: (1) If using Java 7 or above, these are the classes to be used in the `java.nio.file` package: `Paths`, `Path` and `Files`. (2) The code within the `writeLinesToFile` method is duplicated (`FileWriter`, `write` and `close` statements are repeated within the if-else statement). (3) `FileWriter` implements `AutoCloseable` - hence you could use the try-with-resources construct to handle the `IOException` thrown. – prasad_ Jul 06 '18 at 09:11
  • What’s the point of the `if` statement that does the same thing in either case? You can simply do `if(f.createNewFile()) System.out.println("File is created!");` followed by unconditionally do what is common to both cases, though you should use `try(FileWriter writer = new FileWriter(f)) { writer.write(lineConverted); }` to safely close it. Actually, you can omit that obsolete `if` statement altogether and only do `try(FileWriter writer = new FileWriter(f)) { writer.write(lineConverted); }`, which will create the file if it doesn’t exist. – Holger Jul 06 '18 at 15:10

1 Answers1

0

You could handle your exception with a try{} catch(IOException e){}

But it's important to handle the exception, because otherwise your program will do something, but not what you want.

import java.io.*;
public class FileTest {
public static void main(String[] args)
{
    writeLinesToFile("miss.txt", "This is a special file");
}

public static void writeLinesToFile(String outputFileName, String lineConverted){
    File f = new File(outputFileName);
    try {
        if (f.createNewFile()) {
            System.out.println("File is created!");
            FileWriter writer = new FileWriter(f);
            writer.write(lineConverted);
            writer.close();
        } else {
            System.out.println("File already exists.");
            FileWriter writer = new FileWriter(f);
            writer.write(lineConverted);
            writer.close();
        }
    }
    catch(IOException e){
        //Handle your error
    }
}}

But you can't cut out the exceptions at all, because handling files in java throws always exceptions (For example if the file could not be found).