Foros del Web » Programación para mayores de 30 ;) » Java »

Consulta sobre OutputStream

Estas en el tema de Consulta sobre OutputStream en el foro de Java en Foros del Web. Buenas. Estoy estudiando Java y en un ejercicio que se trata de manejar ficheros de texto me piden un método void escribir(OutputStream os). He mirado ...
  #1 (permalink)  
Antiguo 13/11/2014, 18:16
Avatar de jessicamcav13  
Fecha de Ingreso: septiembre-2014
Mensajes: 29
Antigüedad: 9 años, 6 meses
Puntos: 0
Consulta sobre OutputStream

Buenas.

Estoy estudiando Java y en un ejercicio que se trata de manejar ficheros de texto me piden un método void escribir(OutputStream os).

He mirado por internet y aún no me queda claro como hacerlo. Todo lo que intento me da error...

Pongo mi código del ejercicio (El método lo dejaré en blanco porque no se cómo hacerlo).

Cita:
package ejercicio2;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.omg.CORBA.portable.OutputStream;

public class IOFicheroTexto extends JFrame implements ActionListener{

static JTextArea textArea;
static JButton contarLineas;
static JButton copiar;
static JButton eliminar;
static File archivo;
static JScrollPane scrolltxt;
static OutputStream os;

public IOFicheroTexto (String nombre){
super(nombre);
setSize(500, 500);
//setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

contarLineas = new JButton("Contar Lineas");
contarLineas.addActionListener(this);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth= 1;
gbc.gridheight = 1;
gbc.fill= gbc.BOTH;
getContentPane().add(contarLineas,gbc);

copiar = new JButton("Copiar");
copiar.addActionListener(this);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth= 1;
gbc.gridheight = 1;
gbc.fill= gbc.BOTH;
getContentPane().add(copiar,gbc);

eliminar= new JButton("Borrar");
eliminar.addActionListener(this);
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth= 1;
gbc.gridheight = 1;
gbc.fill= gbc.BOTH;
getContentPane().add(eliminar,gbc);

textArea = new JTextArea();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth= 4;
gbc.gridheight = 1;
gbc.weightx=1.0;
gbc.weighty=1.0;
gbc.fill= gbc.BOTH;
getContentPane().add(textArea,gbc);

scrolltxt = new JScrollPane(textArea);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth= 4;
gbc.gridheight = 1;
gbc.weightx=1.0;
gbc.weighty=1.0;
gbc.fill= gbc.BOTH;
getContentPane().add(scrolltxt,gbc);
//scrolltxt.setWheelScrollingEnabled(true);
}


public void escribir(JTextArea textArea){
try{
FileReader leer = new FileReader(archivo);
BufferedReader textoArchivo = new BufferedReader(leer);
String texto;
//System.out.println(textoArchivo.readLine());
while((texto=textoArchivo.readLine()) !=null){
textArea.append(texto + "\n");
}
leer.close();
}catch(FileNotFoundException e){
System.out.println("No se puede leer el archivo");
}catch (IOException e) {
//e.printStackTrace();
System.out.println("No se encuentra el archivo");
}

}

public void escribir(OutputStream os){


}

public int contarLineas(){
int cont=0;
try{
FileReader leer = new FileReader(archivo);
BufferedReader textoArchivo = new BufferedReader(leer);
String texto;

//System.out.println(textoArchivo.readLine());
while((texto=textoArchivo.readLine()) !=null){
cont++;
}
leer.close();
}catch(FileNotFoundException e){
System.out.println("No se puede leer el archivo");
}catch (IOException e) {
//e.printStackTrace();
System.out.println("No se encuentra el archivo");
}
return cont;
}

public void copiar(String nombreFichero){
try {
FileReader leer1 = new FileReader(archivo);
BufferedReader texto1 = new BufferedReader(leer1);
String txt1;
File archivo2 = new File(nombreFichero);
PrintWriter escritor = new PrintWriter(archivo2);

while((txt1 = texto1.readLine())!=null){
escritor.println(txt1);
}
escritor.close();
leer1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Error al abrir el archivo");
}

}

public void eliminar(){

if(archivo.delete()){
JOptionPane.showMessageDialog(null, "Se ha borrado el archivo");
}else{
JOptionPane.showMessageDialog(null, "Error al borrar el archivo");

}

}

public static void main(String [] args){
String ruta=JOptionPane.showInputDialog(null, "Introduzca el nombre del archivo que desea abrir");
if(ruta != null){
archivo= new File (ruta);
IOFicheroTexto ift= new IOFicheroTexto(archivo.getName());
ift.setVisible(true);
ift.escribir(textArea);
}
}

public void actionPerformed(ActionEvent e) {
if(e.getSource() == contarLineas){
JOptionPane.showMessageDialog(null, "Lineas : " +contarLineas());
}

if(e.getSource()==copiar){
String ruta2=JOptionPane.showInputDialog(null, "Introduzca el nombre del archivo que desea abrir");
if(ruta2!=null){
copiar(ruta2);
}
}

if(e.getSource()== eliminar){
eliminar();
}


}

}

Saludos y gracias.
  #2 (permalink)  
Antiguo 14/11/2014, 01:36
Avatar de Profesor_Falken  
Fecha de Ingreso: agosto-2014
Ubicación: Mountain View
Mensajes: 1.323
Antigüedad: 9 años, 7 meses
Puntos: 182
Respuesta: Consulta sobre OutputStream

Buenas,

No explicas nada, pero supongo que en el fichero quieres copiar el contenido del JTextBox.

Seria entonces algo como:


Código Java:
Ver original
  1. public void escribir(OutputStream os){
  2.     byte[] contentInBytes = textArea.getText().getBytes();
  3.     os.write(contentInBytes);
  4.     os.flush();
  5.     os.close();
  6. }


Un saludo
__________________
If to err is human, then programmers are the most human of us

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:29.