Input/Output Java File Chooser

BoreX

Mitglied
Hey ): kann mir wer helfen? ... wir sollen mit nem java FileChooser die umlaute eines textes ändern ^-^... also mit dem filechooser den text auswählen und dann in dem text die umlaute ändern ...

danke schonma im vorraus :)
 

XHelp

Top Contributor
Der FileChooser ändert weder die Umlaute noch wählt er einen Text aus...
Welche konkreten Fragen hast du denn? Und welche eigenen Ansätze hast du?
 

BoreX

Mitglied
also meine konkrete Frage ist: wie ich das mit nem FileChooser umsetzen könnte. Ich bin mitlerweile so weit, dass ich ne Datei aus nem beliebigen Pfad wählen kann jetz müsste noch der teil kommen wie ich die datei auslese und dementsprechend auch mit replace (oder ähnlichem) so verändere dass die umlaute sich durch das programm ändern
 

BoreX

Mitglied
hab ich auch schon gefunden ): kann mir leider nur nich sonderlich bei der programmierung dieses doofen programms helfen >.< .. aber trotzdem danke ich werd weiter versuchen was ich kann ^__^
 

BoreX

Mitglied
sagt eclipse bei bein :( schon ausprobiert ... zumindest nicht in meinem bisherigen programmcode ich häng ihn mal an damit ihr wisst wie weit ich bin ... ich brauch halt nur noch diese doofe änderung der umlaute >.<



Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

//////////////////////////////////////////////////////// CountWords
public class Blah extends JFrame {

    //====================================================== fields
    JTextField   _fileNameTF  = new JTextField(15);
    JTextField   _wordCountTF = new JTextField(4);
    JFileChooser _fileChooser = new JFileChooser();

    //================================================= constructor
    Blah() {
        //... Create / set component characteristics.
        _fileNameTF.setEditable(false);
        _wordCountTF.setEditable(false);

        //... Add listeners

        //... Create content pane, layout components
        JPanel content = new JPanel();
        content.setLayout(new FlowLayout());
        content.add(new JLabel("File:"));
        content.add(_fileNameTF);
        content.add(new JLabel("Word Count:"));
        content.add(_wordCountTF);

        //... Create menu elements (menubar, menu, menu item)
        JMenuBar menubar  = new JMenuBar();
        JMenu    fileMenu = new JMenu("File");
        JMenuItem openItem = new JMenuItem("Open...");
        openItem.addActionListener(new OpenAction());

        //... Assemble the menu
        menubar.add(fileMenu);
        fileMenu.add(openItem);

        //... Set window characteristics
        this.setJMenuBar(menubar);
        this.setContentPane(content);
        this.setTitle("Count Words");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();                      // Layout components.
        this.setLocationRelativeTo(null); // Center window.
    }

    //============================================= countWordsInFile
    private int countWordsInFile(File f) {

        int numberOfWords = 0;  // Count of words.

        try {
            Scanner in = new Scanner(f);

            while (in.hasNext()) {
                String word = in.next();  // Read a "token".
                numberOfWords++;
            }
            in.close();        // Close Scanner's file.

        } catch (FileNotFoundException fnfex) {
            // ... We just got the file from the JFileChooser,
            //     so it's hard to believe there's problem, but...
            JOptionPane.showMessageDialog(Blah.this,
                        fnfex.getMessage());
        }
        return numberOfWords;
    }
    
  
    ///////////////////////////////////////////////////// OpenAction
    class OpenAction implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
            //... Open a file dialog.
            int retval = _fileChooser.showOpenDialog(Blah.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                //... The user selected a file, get it, use it.
                File file = _fileChooser.getSelectedFile();

                //... Update user interface.
                _fileNameTF.setText(file.getName());
                _wordCountTF.setText("" + countWordsInFile(file));
            }
        }
    }
    

    //========================================================= main
    public static void main(String[] args) {
        JFrame window = new Blah();
        window.setVisible(true);
    }
}
 
Zuletzt bearbeitet:

Niki

Top Contributor
hast du glück dass ich gerade nicht ausgelastet bin:
Java:
	public void replaceUmlaute(File f) throws IOException{
		StringBuilder sb = new StringBuilder();
		BufferedReader br = new BufferedReader(new FileReader(f));
		String lineSeparator = System.getProperty("line.separator");
		String line = null;
		while((line = br.readLine()) != null){
			if(sb.length() > 0)
				sb.append(lineSeparator);

			line = line.replaceAll("ä", "ae").
			replaceAll("Ä", "Ae").
			replaceAll("ö", "oe").
			replaceAll("Ö", "Oe").
			replaceAll("ü", "ue").
			replaceAll("Ü", "Ue");
			sb.append(line);
		}
		br.close();

		BufferedWriter bw = new BufferedWriter(new FileWriter(f));
		bw.write(sb.toString());
		bw.flush();
		bw.close();
	}
 

BoreX

Mitglied
dankeschön o: damit wäre meine frage beantwortet :D ich richte mich jederzeit wieder gerne an euch un versuch au so gut wie möglich zu helfen :D
 

Oben