Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 11
  1. #1
    Utente di HTML.it L'avatar di Xadoom
    Registrato dal
    Dec 2002
    Messaggi
    1,491

    [java]Aprire file di testo

    Posso fare in modo che da un programma in java venga aperto un file di testo con il programma predefinito dal SO?
    Ad esempio si apra con blocco note in Windows.
    Windows Xp
    [Java]
    [PHP]Notepad++
    [Fortran90-77] elf90 g77
    [C++ /WinAPI] DevC++ VisualC++

  2. #2
    Utente di HTML.it L'avatar di floyd
    Registrato dal
    Apr 2001
    Messaggi
    3,837
    penso che devi controllare l'estensione e aprire il programma di default, se l'utente cambia l'associazione problema suo

    if (est.equals("txt")) {
    String[] envp = {file};
    Process process = Runtime.getRuntime.exec("notepad", envp);
    }

  3. #3
    Utente di HTML.it L'avatar di Xadoom
    Registrato dal
    Dec 2002
    Messaggi
    1,491
    Scusa, non mi č mai capitato di aprire un altro programma dal mio, mi pare di aver capito che il codice chiave č:
    codice:
    Process process = Runtime.getRuntime.exec("notepad", envp);
    ma a cosa serve la riga precedente!

    Grazie
    Windows Xp
    [Java]
    [PHP]Notepad++
    [Fortran90-77] elf90 g77
    [C++ /WinAPI] DevC++ VisualC++

  4. #4
    Utente di HTML.it L'avatar di floyd
    Registrato dal
    Apr 2001
    Messaggi
    3,837
    serve a impostare i parametri a linea di comando

    notepad file.txt

    con exec va fatto un array dei parametri, in questo caso solo il file

  5. #5
    codice:
    exec public Process exec(String cmd,
                        String[] envp)
                 throws IOException
    
        Executes the specified string command in a separate process with the specified environment.
    
        This method breaks the command string into tokens and creates a new array cmdarray containing the tokens in the order that they were produced by the string tokenizer; it then performs the call exec(cmdarray, envp). The token parsing is done by a StringTokenizer created by the call:
    
     new StringTokenizer(command)
     
    
        with no further modification of the character categories.
    
        The environment variable settings are specified by envp. If envp is null, the subprocess inherits the environment settings of the current process.
    
    Parameters:
    cmd - a specified system command.envp - array of strings, each element of which has environment variable settings in format name=value. Returns:
    a Process object for managing the subprocess. Throws:
    SecurityException - if a security manager exists and its checkExec method doesn't allow creation of a subprocess. IOException - if an I/O error occurs NullPointerException - if cmd is null IllegalArgumentException - if cmd is emptySee Also:
    exec(java.lang.String[]), exec(java.lang.String[], java.lang.String[]), SecurityManager.checkExec(java.lang.String
    Non si legge molto
    Una letturina alle api non farebbe male


    Avrebbe potuto pure scrivere
    codice:
    if (est.equals("txt")) {
     
    Process process = Runtime.getRuntime.exec("notepad", new String[]{file});
    }
    non sarebbe cambiato nulla
    Perchč quel metodo accetta come parametro un array di String

    Ciauz
    Lang=Java
    Ambiente = Eclipse forever
    Ubuntu & Win XP Pro

  6. #6
    Utente di HTML.it L'avatar di Xadoom
    Registrato dal
    Dec 2002
    Messaggi
    1,491
    Scusate un povero autodidatta java ignorante......sigh sigh
    Windows Xp
    [Java]
    [PHP]Notepad++
    [Fortran90-77] elf90 g77
    [C++ /WinAPI] DevC++ VisualC++

  7. #7
    :adhone:
    Scusato :tongue: :tongue: :tongue: :tongue:

    CIauz
    Lang=Java
    Ambiente = Eclipse forever
    Ubuntu & Win XP Pro

  8. #8
    Utente di HTML.it L'avatar di floyd
    Registrato dal
    Apr 2001
    Messaggi
    3,837
    ma solo per stavolta
    :sexpulp: :sexpulp: :sexpulp: :sexpulp: :sexpulp: :sexpulp: :sexpulp:

  9. #9
    Utente di HTML.it L'avatar di Xadoom
    Registrato dal
    Dec 2002
    Messaggi
    1,491
    Non ci riesco....ho inserito questo codice:
    codice:
     
    String[] envp = {" c:/Readme.txt"};
    try{
     Process processo = Runtime.getRuntime().exec("notepad",envp);}
    catch(IOException e){System.out.println(e.toString());}
    tuttavia mi appare una finestra di notepad.exe in cui dice:"applicazione non inizializzata correttamente...."
    Perchč?

    Grazie a tutti
    Windows Xp
    [Java]
    [PHP]Notepad++
    [Fortran90-77] elf90 g77
    [C++ /WinAPI] DevC++ VisualC++

  10. #10
    Utente di HTML.it L'avatar di Xadoom
    Registrato dal
    Dec 2002
    Messaggi
    1,491

    Grazie....

    Ho risolto in questo modo, ma non sō perchč ora va...:quote:
    codice:
      
    try{
     Process processo=Runtime.getRuntime().exec("notepad Readme.txt");
    }
    catch(IOException e){System.out.println(e.toString());}
    ps: mi sono letto le API e ho cpito :gren:

    Grazie ciao
    Windows Xp
    [Java]
    [PHP]Notepad++
    [Fortran90-77] elf90 g77
    [C++ /WinAPI] DevC++ VisualC++

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.