PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Text-Dateien (txt) formatieren


heinzkainz
2009-09-21, 19:09:50
Hallo,

ich suche eigentlich eine ziemlich einfache Funktion, konnte aber nirgends das entsprechende Programm im Internet finden.
Also: Ich habe eine Text-Datei, die kann entweder an einer bestimmten Stelle (z. B. nach max. 80 Zeichen in einer Zeile) einen Zeilenumbruch haben, oder der Text steht einfach in einer ganzen Wurst drinnen. Ich suche nach einem Programm, dass den Text nun automatisch nach meinen Vorgaben formatiert (z. B. Zeilenumbruch nach max. 60 oder 120 Zeichen).

LG

PatkIllA
2009-09-21, 19:25:23
Notepad++ kann das. Wobei sich die Länge der Zeile nach der Fensterbreite richtet.
Ich meine UltraEdit kann das auch mit einstellbarer Länge.

heinzkainz
2009-09-21, 19:44:29
Danke erstmal. Bei Notepad++ hab ich die Funktion leider nicht gefunden. Bei UltraEdit ist die Funktion Format -> Absatzformatierung. Aber gibts vielleicht noch ein anderes Programm, dass das kann? Die Trial von UltraEdit geht ja nur 45 Tage.

PatkIllA
2009-09-21, 19:45:29
bei Notepad++
Edit -> Line Operations -> Split Lines

heinzkainz
2009-09-21, 20:11:26
OK, damit kann man den Zeilenumbruch aber nur früher machen als er ursprünglich war.

PatkIllA
2009-09-21, 20:13:04
OK, damit kann man den Zeilenumbruch aber nur früher machen als er ursprünglich war.
Dann machst du vorher die Umbrüche mit der Funktion oben drunter vorher wieder weg.
Oder halt UltraEdit kaufen.

sloth9
2009-09-21, 21:50:14
Kann jeder Freeware-Texteditor.

heinzkainz
2009-09-21, 22:59:25
So, hab jetzt einfach selbst ein Programm geschrieben:

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

public class TxtFormat extends JFrame implements Runnable {

private static final long serialVersionUID = 1L;

Vector<File> files;
int width, encoding;

JFileChooser fchChooser;
JScrollPane scpFiles;
JList lstFiles;
JButton btnAdd, btnRem, btnOK;
JTextField tfdWidth;
JComboBox cmbEncoding;

JDialog dlgProgress;
JProgressBar prbFile, prbAll;

public TxtFormat() {
setTitle("TxtFormat");
setSize(640, 480);
setResizable(false);
setLocation((getToolkit().getScreenSize().width - getWidth()) / 2,
(getToolkit().getScreenSize().height - getHeight()) / 2);
setDefaultCloseOperation(EXIT_ON_CLOSE);

files = new Vector<File>();

fchChooser = new JFileChooser();
fchChooser.setMultiSelectionEnabled(true);
lstFiles = new JList();
scpFiles = new JScrollPane(lstFiles,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
btnAdd = new JButton("+");
btnRem = new JButton("-");
btnOK = new JButton("OK");
tfdWidth = new JTextField("80");
cmbEncoding = new JComboBox(new String[] { "US-ASCII", "ISO-8859-1",
"UTF-8", "UTF-16BE", "UTF-16LE", "UTF-16" });
cmbEncoding.setSelectedIndex(1);

dlgProgress = new JDialog(this, "Formatting files...", true);
dlgProgress.setSize(400, 80);
dlgProgress.setResizable(false);
dlgProgress.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
prbFile = new JProgressBar(0, 100);
prbAll = new JProgressBar(0, 100);
dlgProgress.add(prbFile, BorderLayout.CENTER);
dlgProgress.add(prbAll, BorderLayout.SOUTH);

btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fchChooser.showOpenDialog(TxtFormat.this);
File[] tmp = fchChooser.getSelectedFiles();
if(tmp == null || tmp.length <= 0)
return;
for(File x : tmp)
files.add(x);
lstFiles.setListData(files);
}
});
btnRem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[] ind = lstFiles.getSelectedIndices();
int dec = 0;
for(int x : ind) {
files.remove(x - dec);
dec++;
}
lstFiles.setListData(files);
}
});
btnOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
format();
}
});

scpFiles.setBounds(10, 10, 605, 390);
btnAdd.setBounds(10, 410, 100, 20);
btnRem.setBounds(120, 410, 100, 20);
tfdWidth.setBounds(240, 410, 100, 20);
cmbEncoding.setBounds(360, 410, 145, 20);
btnOK.setBounds(515, 410, 100, 20);

getContentPane().setLayout(null);
getContentPane().add(scpFiles);
getContentPane().add(btnAdd);
getContentPane().add(btnRem);
getContentPane().add(tfdWidth);
getContentPane().add(cmbEncoding);
getContentPane().add(btnOK);

setVisible(true);
}

public void format() {
if(files.size() <= 0) {
JOptionPane.showMessageDialog(this, "You have to select some files",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
width = Integer.parseInt(tfdWidth.getText());
if(width < 0)
throw new Exception();
}
catch(Exception e) {
JOptionPane.showMessageDialog(this,
"Enter an integer >=0 for text width", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
encoding = cmbEncoding.getSelectedIndex();

prbAll.setMaximum(files.size());
prbAll.setValue(0);
dlgProgress.setLocation(
getLocation().x + (getWidth() - dlgProgress.getWidth()) / 2,
getLocation().y + (getHeight() - dlgProgress.getHeight()) / 2);
new Thread(this).start();
dlgProgress.setVisible(true);
}

public void run() {
for(File f : files) {
try {
prbFile.setValue(0);
prbFile.setMaximum((int)f.length());
StringBuffer out = new StringBuffer();
FileInputStream fis = new FileInputStream(f);
DataInput di = new DataInputStream(fis);
StringBuffer buf = new StringBuffer();
String str = readAndTrim(di);
while(str != null) {
if(str.length() <= 0) {
if(buf.length() > 0) {
out.append(buf.toString());
out.append("\r\n");
buf.delete(0, buf.length());
}
out.append("\r\n");
}
else {
if(buf.length() > 0)
buf.append(' ');
buf.append(str);
while(width > 0 && buf.length() >= width) {
int ind = buf.lastIndexOf(" ", width);
if(ind < 0) {
out.append(buf.substring(0, width));
buf.delete(0, width);
}
else {
out.append(buf.substring(0, ind));
buf.delete(0, ind + 1);
}
out.append("\r\n");
}
}
str = readAndTrim(di);
}
if(buf.length() > 0)
out.append(buf);
fis.close();
f.renameTo(new File(f.toString() + ".bak"));
FileOutputStream fos = new FileOutputStream(f);
fos.write(out.toString().getBytes(
cmbEncoding.getSelectedItem().toString()));
}
catch(Exception e) {
JOptionPane.showMessageDialog(this,
"Problem with \"" + f + ": \"" + e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
prbAll.setValue(prbAll.getValue() + 1);
}
dlgProgress.setVisible(false);
}

public String readAndTrim(DataInput di) throws IOException {
String s = di.readLine();
if(s == null)
return null;
prbFile.setValue(prbFile.getValue() + s.length() + 1);
int i;
for(i = s.length() - 1; i >= 0; i--) {
if(!Character.isWhitespace(s.charAt(i)))
return s.substring(0, i + 1);
}
return "";
}

public static void main(String[] args) {
new TxtFormat();
}
}

Wenn man 0 in das Textfeld eingibt werden die Zeilenumbrüche entfernt.

Wäre dankbar über Hinweise auf Fehler.