|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Feb 2006
Messaggi: 79
|
[java] log4j e altra domanda
ciao a tutti ho due domande da porvi:
devo creare un file solo che il path me lo deve dare l'utente,come posso fare x farmi dare il path in maniera tale da creare in questa cartella il file? seconda domanda: devo utilizzare il log4j, solo che non so di cosa tratti,qualcuno mi può dare una mano??? grazie. ciao ciao |
|
|
|
|
|
#2 | |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 5206
|
Quote:
__________________
Andrea, Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%) Java Versions Cheat Sheet |
|
|
|
|
|
|
#3 |
|
Member
Iscritto dal: Feb 2006
Messaggi: 79
|
lui mi da un path fisso quindi non lo cambia ogni volta, e quindi in quella cartella io devo creare il file.
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Feb 2003
Città: GE
Messaggi: 397
|
Ma è un'applicazione grafica? testuale? web?
Comunque una volta che hai l'input in una stringa concatenala coin il nome del file e poi lo puoi creare. per quanto riguarda log4j è un framework di logging mantenuto dalla fondazione apache (basta scrivere log4j su google...)
__________________
La supposizione e' la madre di tutte le ca**ate! |
|
|
|
|
|
#5 |
|
Member
Iscritto dal: Feb 2006
Messaggi: 79
|
è un semplice programma che riceve due stringhe in ingresso e se il file non esiste viene generato, altrimenti le due stringhe vengono concatenate al file già esistente.
|
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 5206
|
Puoi usare i file di "properties". Crei un file es. miaapp.properties con dentro:
path = xxx... poi lo carichi con: Codice:
Properties props = new Properties ();
try {
props.load (new FileInputStream ("miaapp.properties"));
String path = props.getProperty ("path");
} catch (IOException e) {
/* .... */
}
__________________
Andrea, Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%) Java Versions Cheat Sheet |
|
|
|
|
|
#7 |
|
Member
Iscritto dal: Feb 2006
Messaggi: 79
|
Codice:
import java.io.*;
public class bo
{
public static void main (String[] args)
{
Properties props = new Properties ();
try
{
props.load (new FileInputStream ("miaapp.properties"));
String path = props.getProperty ("path");
}
catch (IO.Exception e)
{
System.out.println("Ciao");
}
}
}
p.s. ho creato un file che contiene semplicemente path = nome path |
|
|
|
|
|
#8 | |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 5206
|
Quote:
__________________
Andrea, Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%) Java Versions Cheat Sheet |
|
|
|
|
|
|
#9 |
|
Member
Iscritto dal: Feb 2006
Messaggi: 79
|
Codice:
public static void inserisci (String c,char a)
{
{
char risp;
char coda;
File f= new File ("testa.txt");
if (!f.isFile())
{
try
{
if ((!c.equals ("")) && (c != null))
{
PrintWriter pw = new PrintWriter (new FileOutputStream ("testa.txt"));
//pw.println ();
//pw.println();
pw.println (c+" "+a);
pw.close ();
}
else
throw new errore_stringa();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
else
{
/*System.out.println("Vuoi mettere in coda la stringa al file gia esistente?s/n");
coda=SavitchIn.readChar();
while ((coda != 's') && (coda != 'n'))
{
System.out.println("Vuoi mettere in coda la stringa al file gia esistente?s/n");
coda=SavitchIn.readChar();
}*/
//if (coda == 's')
{
try
{
BufferedWriter bw = new BufferedWriter (new FileWriter ("testa.txt", true));
if ((!c.equals ("")) && (c != null))
{
bw.write (c+" "+a);
bw.newLine();
bw.close ();
}
else
throw new errore_stringa();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
/*else
{
//System.out.println("Vuoi sovrascrivere il file?s/n");
risp=SavitchIn.readChar();
while ((risp!='s')&& (risp!='n'))
{
System.out.println("Vuoi sovrascrivere il file?s/n");
risp=SavitchIn.readChar();
}
if (risp=='s')
{
try
{
if ((!c.equals ("")) && (c != null))
{
PrintWriter pw = new PrintWriter (new FileOutputStream ("testa.txt"));
pw.println (c);
pw.close ();
}
else
throw new errore_stringa();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
}*/
}
}
}
}
class errore_stringa extends Exception
{
errore_stringa()
{
super ("La stringa che mi e' stata passata e' vuota");
}
}
|
|
|
|
|
|
#10 | |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 5206
|
Quote:
Codice:
import java.io.*;
import java.util.*;
public class MiaApplicazione
{
private String path;
public static void main (String[] args)
{
try
{
MiaApplicazione miaApp = new MiaApplicazione ();
miaApp.loadConfig ();
/*
...
chiama miaApp.inserisci (...);
...
*/
}
catch (Exception e)
{
e.printStackTrace ();
}
}
public void loadConfig ()
throws IOException
{
Properties props = new Properties ();
props.load (new FileInputStream ("miaapp.properties"));
path = props.getProperty ("path");
}
public void inserisci (String c, char a)
{
/* .... */
}
}
__________________
Andrea, Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%) Java Versions Cheat Sheet |
|
|
|
|
|
|
#11 |
|
Member
Iscritto dal: Feb 2006
Messaggi: 79
|
Codice:
import java.io.*;
import java.util.*;
public class cop2
{
public static void main (String[] args)
{
String c;
char a;
System.out.println ("dammi la stringa da inserire nel file");
c= SavitchIn.readLine();
System.out.println ("dammi il carattere da inserire nel file");
a= SavitchIn.readChar();
File d = new File ("esolutions");
d.mkdir ();
miaApp.inserisci(c,a);
}
public class MiaApplicazione
{
private String path;
{
try
{
MiaApplicazione miaApp= new MiaApplicazione ();
miaApp.loadConfig ();
//miaApp.inserisci(c,a);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public void loadConfig ()
throws IOException
{
Properties props = new Properties ();
props.load (new FileInputStream ("miaapp.properties"));
String path = props.getProperty ("path");
}
public static void inserisci (String c,char a)
{
{
char risp;
char coda;
File f= new File ("esolutions/testa.txt");
if (!f.isFile())
{
try
{
if ((!c.equals ("")) && (c != null))
{
PrintWriter pw = new PrintWriter (new FileOutputStream ("esolutions/testa.txt"));
//pw.println ();
//pw.println();
pw.println (c+" "+a);
pw.close ();
}
else
throw new errore_stringa();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
else
{
/*System.out.println("Vuoi mettere in coda la stringa al file gia esistente?s/n");
coda=SavitchIn.readChar();
while ((coda != 's') && (coda != 'n'))
{
System.out.println("Vuoi mettere in coda la stringa al file gia esistente?s/n");
coda=SavitchIn.readChar();
}*/
//if (coda == 's')
{
try
{
BufferedWriter bw = new BufferedWriter (new FileWriter ("esolutions/testa.txt", true));
if ((!c.equals ("")) && (c != null))
{
bw.write (c+" "+a);
bw.newLine();
bw.close ();
}
else
throw new errore_stringa();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
/*else
{
//System.out.println("Vuoi sovrascrivere il file?s/n");
risp=SavitchIn.readChar();
while ((risp!='s')&& (risp!='n'))
{
System.out.println("Vuoi sovrascrivere il file?s/n");
risp=SavitchIn.readChar();
}
if (risp=='s')
{
try
{
if ((!c.equals ("")) && (c != null))
{
PrintWriter pw = new PrintWriter (new FileOutputStream ("esolutions/testa.txt"));
pw.println (c);
pw.close ();
}
else
throw new errore_stringa();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
}*/
}
}
}
}
class errore_stringa extends Exception
{
errore_stringa()
{
super ("La stringa che mi e' stata passata e' vuota");
}
}
|
|
|
|
|
|
#12 | |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 5206
|
Quote:
Ecco: Codice:
import java.io.*;
import java.util.*;
public class cop2
{
private String path;
public static void main (String[] args)
{
try
{
cop2 cop = new cop2 ();
cop.loadConfig ();
String c;
char a;
System.out.println ("dammi la stringa da inserire nel file");
c= SavitchIn.readLine();
System.out.println ("dammi il carattere da inserire nel file");
a= SavitchIn.readChar();
File d = new File ("esolutions");
d.mkdir ();
cop.inserisci (c,a);
}
catch (Exception e)
{
e.printStackTrace ();
}
}
public void loadConfig ()
throws IOException
{
Properties props = new Properties ();
props.load (new FileInputStream ("miaapp.properties"));
String path = props.getProperty ("path");
}
public void inserisci (String c,char a)
{
/* .... */
}
}
/* .... */
__________________
Andrea, Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%) Java Versions Cheat Sheet |
|
|
|
|
|
|
#13 |
|
Member
Iscritto dal: Feb 2006
Messaggi: 79
|
scusa un'ultima domanda: come faccio per ogni riga che scrivo nel file a inserire la data nel formato gg/mm/aaaa all'ora hh:mm?
|
|
|
|
|
|
#14 | |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 5206
|
Quote:
Codice:
import java.text.*;
import java.util.*;
....
DateFormat fmt = new SimpleDateFormat ("dd/MM/yyyy HH:mm");
String stringaData = fmt.format (new Date ());
__________________
Andrea, Senior Java developer – SCJP 5 (91%) • SCWCD 5 (94%) Java Versions Cheat Sheet |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 11:38.



















