Visualizzazione dei risultati da 1 a 10 su 14

Visualizzazione discussione

  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2013
    Messaggi
    68

    Non legge correttamente un file di testo

    Ciao a tutti, sto impazzendo.
    Praticamente devo leggere un file di testo, che è formato solo da una riga, e devo visualizzarla in una parte grafica, sto utilizzando windowBuilder per aiutarmi nella realizzazione di una parte grafica.

    Vi descrivo cosa fa, legge da un file di testo la riga, e ogni 5 caratteri li inserisce in un Text.
    Non funziona, come stringa letta del file mi da questo:
    codice:
    ÿþ1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
    anche se il file di testo è
    codice:
    12345678901234567890
    vi riporto il codice
    codice:
    package main;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    
    public class Dati 
    {
    
    
        public Dati()
        {
            dati = new String("");
            i = 0;
        }
        
        public String dati;
        private int i;
        
        public String letturaFile() throws IOException
        {
            
            
            BufferedReader reader = new BufferedReader( new FileReader("c:/mio.dat") );
            dati = reader.readLine();
            
            System.out.print(dati);
            
            i = i +5;
            if (i>=20) i = 0;
            reader.close();
            return dati.substring(i, i+5);
            
            
            
        }
    mentre per la parte di interfaccia

    codice:
    package main;
    
    import java.io.IOException;
    
    
    public class Rubrica extends ApplicationWindow {
        private Text text;
        private Text text_1;
        private Text text_2;
        private Text text_3;
    
    
        /**
         * Create the application window.
         */
        public Rubrica() {
            super(null);
            createActions();
            addToolBar(SWT.FLAT | SWT.WRAP);
            addMenuBar();
            addStatusLine();
        }
    
    
        /**
         * Create contents of the application window.
         * @param parent
         */
        @Override
        protected Control createContents(Composite parent)
        {
            Dati dati = new Dati();
            
            Composite container = new Composite(parent, SWT.NONE);
            
            Label lblNome = new Label(container, SWT.NONE);
            lblNome.setBounds(10, 10, 55, 15);
            lblNome.setText("Nome");
            
            text = new Text(container, SWT.BORDER);
            text.setBounds(70, 4, 98, 21);
            
            try {
                text.setText(dati.letturaFile());
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
            Label lblCognome = new Label(container, SWT.NONE);
            lblCognome.setBounds(187, 10, 55, 15);
            lblCognome.setText("Cognome");
            
            text_1 = new Text(container, SWT.BORDER);
            text_1.setBounds(262, 4, 108, 21);
            try {
                text_1.setText(dati.letturaFile());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    
            
            Label lblNome_1 = new Label(container, SWT.NONE);
            lblNome_1.setBounds(10, 46, 55, 15);
            lblNome_1.setText("Nome");
            
            text_2 = new Text(container, SWT.BORDER);
            text_2.setBounds(70, 40, 98, 21);
            try {
                text_2.setText(dati.letturaFile());
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        
            
            Label lblCognome_1 = new Label(container, SWT.NONE);
            lblCognome_1.setBounds(187, 46, 55, 15);
            lblCognome_1.setText("Cognome");
            
            text_3 = new Text(container, SWT.BORDER);
            text_3.setBounds(262, 40, 108, 21);
            try {
                text_3.setText(dati.letturaFile());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            return container;
        }
    
    
        /**
         * Create the actions.
         */
        private void createActions() {
            // Create the actions
        }
    
    
        /**
         * Create the menu manager.
         * @return the menu manager
         */
        @Override
        protected MenuManager createMenuManager() {
            MenuManager menuManager = new MenuManager("menu");
            return menuManager;
        }
    
    
        /**
         * Create the toolbar manager.
         * @return the toolbar manager
         */
        @Override
        protected ToolBarManager createToolBarManager(int style) {
            ToolBarManager toolBarManager = new ToolBarManager(style);
            return toolBarManager;
        }
    
    
        /**
         * Create the status line manager.
         * @return the status line manager
         */
        @Override
        protected StatusLineManager createStatusLineManager() {
            StatusLineManager statusLineManager = new StatusLineManager();
            return statusLineManager;
        }
    
    
        /**
         * Launch the application.
         * @param args
         */
        public static void main(String args[]) {
            try {
                Rubrica window = new Rubrica();
                window.setBlockOnOpen(true);
                window.open();
                Display.getCurrent().dispose();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
        /**
         * Configure the shell.
         * @param newShell
         */
        @Override
        protected void configureShell(Shell newShell) {
            super.configureShell(newShell);
            newShell.setText("Gestione Rubrica");
        }
    
    
        /**
         * Return the initial size of the window.
         */
        @Override
        protected Point getInitialSize() {
            return new Point(450, 300);
        }
    }
    questa è la foto dell'interfaccia grafica, in ogni casella di testo dovrebbero esserci 5 numeri, ma non è così. Riuscite ad aiutarmi?
    https://www.dropbox.com/s/qxj5rxtbhk3z874/Immagine.jpg
    Ultima modifica di Maxton; 25-03-2014 a 17:38

Tag per questa discussione

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.