简体   繁体   中英

I am trying to send File as a parameter to a method but dont know how?

public static void createFile() {

    File file = new File("C:\\Users\\egecoskun\\Desktop\\javafiles\\ananınamı.txt");
    try {
        if (file.createNewFile()) {
            System.out.println("Dosya olusturuldu!");

        } else {
            System.out.println("Dosya zaten var!");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Here is my createFile method to create a file.

public static void readFile(File file) {
    try {
        Scanner reader = new Scanner(file);
        while(reader.hasNextLine()) {
            String line=reader.nextLine();
            System.out.println(line);
        }
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    }

}

and this method is reading the file i am creating.But it needs to take File as an argument but when i try to run this method in Main

public static void main(String[] args) {
    createFile();
    readFile(file);

}

The error i am getting is file cannot be resolved to a variable. Does anyone spot my mistake? Can you explain it please.

Return file object from createFile Function and pass it to readFile Function

public static File createFile() {
    File dir = new File("C:\\Users\\egecoskun\\Desktop\\javafiles");
    if(dir.exists()) {
       dir.mkdirs();
    }
    File file = new File(dir,"ananınamı.txt");
    file.createNewFile();
    return file;
}

In your main function

File file = createFile();
readFile(file);

createFile is void ; that means it does not return anything. In this case, you want to return a File . Like,

public static File createFile() {
    File file = new File("C:\\Users\\egecoskun\\Desktop\\javafiles\\ananınamı.txt");
    try {
        if (file.createNewFile()) {
            System.out.println("Dosya olusturuldu!");
            return file;
        } else {
            System.out.println("Dosya zaten var!");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

Then you need to assign the result of calling that method to a variable. And you should check that it is not null before you pass it to readFile . Like,

File f = createFile();
if (f != null) {
    readFile(f);
}

Note that the file won't actually contain anything to read. It just exists after you created it.

import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCall2 {
    public static void CALL(FileOutputStream fr, int w) {
       try {
         fr.write(w);
       } catch (Exception e) {
        System.out.println("Exception: " + e);
       }
    }
  public static void main(String[] args) {
     try (FileInputStream f = new FileInputStream("c:\\code\\a.txt")) {
        FileOutputStream f2 = new FileOutputStream("c:\\code\\a2.txt");
      int c;
      char wd;
      while((c = f.read()) != -1) {
          System.out.print((char)c);
          wd = (char)c;
          CALL(f2, c);
          }
      f.close();              
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM