0

I have question about how to create file and append it.

what i have is String getDate = "2020-04-02" for name file. so my file will create every day if the date different one so my format for save file is

2020-04-02.text and for tomorrow my data will save in 2020-04-03.

after that my input data is

String savefile ="aaaa|bbbb|2020-04-02T08:18:35.146"

my question is how to create file and append my data to fix save file ?

i have try using

      System.out.println("savelocation: " + paths);
      Files.write(Paths.get(paths), savefile.getBytes(StandardCharsets.UTF_8), 
              StandardOpenOption.CREATE, StandardOpenOption.APPEND);

but i got error like this

java.nio.file.NoSuchFileException: /home/ec2-user/saveFile/EUR/USD|2020-04-02.txt
        at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
        at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
        at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:434)

my expected code is : 1. Check file path it is exist or not if it is exist append data 2. if file path it is not exist create new file and write new data

regards

Fuad

fuad trix
  • 33
  • 8

3 Answers3

0

You can learn about your question visit below URL. https://www.w3schools.com/java/java_files_create.asp https://www.w3schools.com/java/java_files_read.asp

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;

Writer output;
output = new BufferedWriter(new FileWriter(my_file_name,true));
output.append("New Line!");
output.close();

It is from below URL.

How to add a new line of text to an existing file in Java?

I hope it helps you.

  • hi i want to writeln file.... how to writeln and append it using java ? – fuad trix Apr 02 '20 at 09:54
  • import java.io.BufferedWriter; import java.io.FileWriter; import java.io.Writer; Writer output; output = new BufferedWriter(new FileWriter(my_file_name,true)); output.append("New Line!"); output.close(); It is from below URL. https://stackoverflow.com/questions/4614227/how-to-add-a-new-line-of-text-to-an-existing-file-in-java I hope it helps you. – 서민봉 Minbong David Seo Apr 02 '20 at 10:00
  • yes your code is for append but how to create new file before have the file ? – fuad trix Apr 02 '20 at 11:03
0

You can use java.time.LocalDate class to create the filename. If you want to append data if the file exists then you can send second parameter to the FileWriter object as true It opens the file in appending mode.

You can check file exists or not using a method exists() in File class.

import java.io.*;

public class Main
{
    public static void main(String[] args) throws IOException {

        String savefile ="aaaa|bbbb|" + java.time.LocalDateTime.now().toString();

        String fileName = java.time.LocalDate.now().toString() + ".txt"; 

        File file = new File(fileName);
        if(file.exists() && !file.isDirectory()){
            FileWriter fw = new FileWriter(fileName, true);
            fw.write("\n");
            fw.write(savefile);
            fw.close();
        } else{
            FileWriter fw = new FileWriter(fileName);
            fw.write(savefile);
            fw.close();
        }


    }
}
deadshot
  • 8,881
  • 4
  • 20
  • 39
  • oh i see but for append how to create new line in that file @Raju Komati – fuad trix Apr 02 '20 at 10:12
  • you can add new line using `fw.write("\n")` then you can append data using `fw.write(data)` method. – deadshot Apr 02 '20 at 10:16
  • hi @Raju Komati i have update my expected code my expected code is : 1. Check file path it is exist or not if it is exist append data 2. if file path it is not exist create new file and write new data – fuad trix Apr 02 '20 at 11:38
0

hi all thanks for all of you this problem done... with this code

 System.out.println("savelocation: " + paths);
  Files.write(Paths.get(paths), savefile.getBytes(StandardCharsets.UTF_8), 
          StandardOpenOption.CREATE, StandardOpenOption.APPEND);

thanks for all of you... i miss the function about path in linux

fuad trix
  • 33
  • 8