Difficulty defining the path where a particular txt will be created

Asked

Viewed 333 times

1

The method below generates a txt containing certain information:

public static void gravarIp(String ip)
{   
    try {
        File arquivo = new File("ip.txt");  
        FileOutputStream fos = new FileOutputStream(arquivo);  
        String texto = ip;  
        fos.write(texto.getBytes());  
        fos.close();
        atualizarPortal(ip);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

However, this txt is being generated at the place where the instruction is executed:

c:\>java -cp c:\users\fabio\desktop EnviarIp

That is, if the above statement is executed and I am at the root of c: txt will be generated there. How do I generate txt on the same root as the Send file without specifying the absolute path in the source code?

  • First, what is it EnviarIp? Nothing in the code indicates this. Following, because you cannot specify the absolute path?

  • I only published part of the code, "Enviar ip" is the java class, as it should already be known to run a java code we can use the java command line -cp path_do_file File. I cannot inform the way because it will be relative according to user profile and other factors.

2 answers

2


The most I could was to generate a file in a fixed location like the User folder (it can be a subfolder inside it if you want) or an Absolute Path as C disk:, Program Files folder etc.

Stay like this:

import java.io.*;

public class Teste {
    public static void main(String[] args) {        
        try {       
            File arquivo = new File(System.getProperty("user.home"), "ip.txt");  
            FileOutputStream fos = new FileOutputStream(arquivo);  
            String texto = "192.168.1.1";  
            fos.write(texto.getBytes());  
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Unfortunately I could not generate the file in the same location as the class/jar.


Answer found with the help of the question user himself:

import java.io.*;

public class Teste {
    public static void main(String[] args) {        
        try {
            String diretorio = System.getProperty("user.dir");      
            File arquivo = new File(diretorio, "ip.txt");  
            FileOutputStream fos = new FileOutputStream(arquivo);  
            String texto = "192.168.1.1";  
            fos.write(texto.getBytes());  
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I put in the Github for future reference.

Searching the property System.getProperty("user.dir") is the directory where the class is. Sorry I didn’t find out earlier and I thank you.

Source: System Properties - Java

  • 1

    Dear friend, thank you for your help, I managed to solve the problem by doing: "String directory = System.getProperty("user.dir");" and then informing this variable as part of the path. That considering the example you set :)

  • That’s right, I don’t know how I didn’t see it before. Follow the edited reply. Thank you.

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.