Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    scrivere nuova linea in un file

    Ciao,
    ho implementato una funzione che leggendo un certo file mi crea all'interno di uno nuovo, le stesse righe del file di testo aggiungendo una stringa in un punto preciso, pero' ho alcuni problemi in fase di lettura (credo)...
    Questo è il codice della mia funzione dove, il metodo "getTagsBaseName()" mi dovrebbe restituire una stringa creata in precedenza, ed andarla ad inserire nel nuovo file...
    codice:
    public class ReadWriteTemp {
    		
    		MainArray ma = new MainArray();
    		String TopicLine = "<resourceData>Topic Di Bamberg Symphony</resourceData>";
        
        public ReadWriteTemp() {
            
            BufferedReader br = null;
            BufferedWriter bw = null;
            
            try {
                br = new BufferedReader(new FileReader("TapsasConcerts_Bamberg.xtm"));
            } catch (FileNotFoundException e) {
                System.err.println("File Tapsas Concerts Bamberg not found!");
                System.exit(1);
            }
            
            try {
                bw = new BufferedWriter(new FileWriter("out.xtm"));
            } catch (IOException e) {
                System.err.println("Error opening out.txt!");
                System.exit(1);
            }
            
            try {
                while(br.ready()) {
                    
                    String inLine = br.readLine();
                    
                    if(inLine.equals(TopicLine)) {
                        bw.write(ma.getTagsBaseName());
                        bw.newLine();
                    }
                    
                    bw.write(inLine);
                    bw.newLine();                
                }
            } catch (IOException e) {
                System.err.println("Error reading or writing!");
                System.exit(1);
            }
            
    
            try {
                br.close();
                bw.close();
            } catch (IOException e) {
                System.err.println("Error closing!");
                System.exit(1);
            }
        }
        
        public static void main(String args[]) {
            
            new ReadWriteTemp();
        }
    }
    Questo è invece il file preso in input:

    codice:
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <topicMap xmlns="http://www.topicmaps.org/xtm/1.0/" xmlns:xlink="http://www.w3.org/1999/xlink" id="id0">
    
      <topic id="id596">
        <instanceOf>
          <topicRef xlink:href="#id2"></topicRef>
        </instanceOf>
        <occurrence>
          <instanceOf>
            <topicRef xlink:href="#id7"></topicRef>
          </instanceOf>
          <resourceData>2006-12-14</resourceData>
        </occurrence>
        <occurrence>
          <instanceOf>
            <topicRef xlink:href="#id5"></topicRef>
          </instanceOf>
          <resourceData>I picked this concert from a program of events that Erika had waiting for us when we arrived for a five-day pre-Christmas visit and we managed to get tickets for the four of us. The Meistersingerhalle is enormous and in the fourth row of the balcony the orchestra feels miles away. Fortunately the acoustics are quite good. I found Blomstedt's tempi to be a bit on the slow side: it's as if he loves the music so much he doesn't want it to ever end, but I would have preferred a bit more forward motion and - given the size of the hall - a bit more power as well. Nevertheless, a very enjoyable concert.</resourceData>
        </occurrence>
        <occurrence>
          <instanceOf>
            <topicRef xlink:href="#id61"></topicRef>
          </instanceOf>
          <resourceData>Topic Di Bamberg Symphony</resourceData>
        </occurrence>
      </topic>
    In sostanza dovrei inserire un stringa del tipo:
    <baseName>
    <baseNameString>Bamberg Symphony 2006-12-14</baseNameString>
    </baseName>
    all'interno del mio Topic...

  2. #2
    Utente di HTML.it L'avatar di Ed_Bunker
    Registrato dal
    Jul 2003
    Messaggi
    1,119
    Non indichi cosa sia TopicLine e cmq. dovresti invocare, almeno alla fine del ciclo di lettura/scrittura, il metodo flush() del buffer.

  3. #3
    non capisco che cosa intendi quando dici che non indico cosa sia TopicLine visto che l'ho passato come stringa all'inizio...
    codice:
    String TopicLine = "<resourceData>Topic Di Bamberg Symphony</resourceData>";
    il metodo flush() è cosi indispensabile?? se si perchè???
    grazie

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 © 2025 vBulletin Solutions, Inc. All rights reserved.