Download CODIGO PARA JAVA-----------------

Document related concepts
no text concepts found
Transcript
CODIGO PARA JAVA-----------------PROGRAMA PARA CREAR UN ARCHVO TXT
import java.io.*;
public class Crear_archivo {
File f;
public static void main (String[] args){
try {
FileWriter fw = new FileWriter("n_primos.txt");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter salida = new PrintWriter(bw);
salida.println("esto escribe una linea en el archivo de diego baroja llanos");
salida.close();
}
catch(java.io.IOException ioex) {
System.out.println("se presento el error: "+ioex.toString());
}
}
}
CODIGO PARA ESCRIBIR UN PROGRAMA TXT
import java.io.*;//nunca olvidemos importar las librerias necesarias
public class Escribir_Texto {
public static void main(String args[]){
//si trabajamos archivos no olvidemos el try y catch
try{
FileWriter fw = new FileWriter("archivo.txt");
BufferedWriter bw = new BufferedWriter(fw);
String texto = "hola\nesto es un ejemplo\nde archivo de texto, ccomprobado .";
String linea = "";
//texto.length() es el numero de caracteres que contiene el string
for (int i = 0; i < texto.length(); i++){
/***********************************************************\
*si el caracter en la posicion i es diferente de enter scribimos caracter por caracter y luego
cuando el *
*lleguemos a un enter entonces en el archivo escribimos una linea nueva.
*
\***********************************************************/
if (texto.charAt(i) != '\n')
bw.write(texto.charAt(i));
else
bw.newLine();//de esta manera es como cambiamos de linea
}
bw.close();
fw.close();
//nunca nos olvidemos de cerrar los archivos.
}catch(Exception e){}
}
}
PROGRAMA PARA LEER UN ARCHIVO DE TEXTO.
import java.io.*;
public class leeFichero {
public static void main(String [] arg) {
File archivo = null;
FileReader fr = null;
BufferedReader br = null;
try {
// Apertura del fichero y creacion de BufferedReader para poder
// hacer una lectura comoda (disponer del metodo readLine()).
archivo = new File ("plano.txt");
fr = new FileReader (archivo);
br = new BufferedReader(fr);
// Lectura del fichero
String linea;
while((linea=br.readLine())!=null)
System.out.println(linea);
}
catch(Exception e){
e.printStackTrace();
}finally{
// En el finally cerramos el fichero, para asegurarnos
// que se cierra tanto si todo va bien como si salta
// una excepcion.
try{
if( null != fr ){
fr.close();
}
}catch (Exception e2){
e2.printStackTrace();
}
}
}
}
CODIGO PROGRAMA PARA COPIAR ARCHIVOS
import java.io.*;//nunca olvidemos importar las librerias
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class copiar_archivos {
public static void main(String args[]){
//el manejo de archivos siempre debe ir dentro de un try y catch
try{
//Para copiar archivos no importa que clase de archivo sea
//es el archivo que vamos a copiar si esta en otra carpeta
//debemos poner toda la ubicacion
FileInputStream fIn = new FileInputStream("cajita1.bmp"");
//es la copia del archivo si lo queremos en otra carpeta
//debemos poner toda la ubicacion
FileOutputStream fOut = new FileOutputStream("copia.bmp"");
FileChannel fIChan = fIn.getChannel();
FileChannel fOChan = fOut.getChannel();
long fSize = fIChan.size();
MappedByteBuffer mBuf =
fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
fOChan.write(mBuf);//con esto copiamos el archivo
//nunca olvidemos cerrar los archivos
fIChan.close();
fIn.close();
fOChan.close();
fOut.close();
}catch(Exception ef){}
}
}
CODIGO PARA ELIMINAR UN FICHERO
import java.io.File;//nunca olviden importar las librerias
public class Eliminar_Archivos {
public static void (String[] args)
{
//si el archivo no esta en la misma carpetas
//debemos poner toda la direccion
File archivo = new File("binary.ext");
archivo.delete();//esto elimina el archivo
}
CODIGO PARA ESCRIBIR UN ARCHIVO BINARIO
import java.io.*;//nunca olvidemos importar las librerias necesarias
public class escribir_binario {
public static void main(String args[]){
//el manejo de archivos siempre debe ir dentro de un try y catch
try{
FileOutputStream fo = new FileOutputStream("binary.ext");
ObjectOutputStream os = new ObjectOutputStream(fo);
String texto = "Ejemplo\nde\narchivo\nbinario.";
os.writeObject(texto);
/***********************************************************\
* si abrimos el archivo creado con un editor de texto *
* lo que veremos seran muchos caracteres raros pero si *
* lo abrimos desde java lo podremos leer sin problemas *
* en un archivo binario podemos guardar cualquier objeto *
\***********************************************************/
os.close();
fo.close();
}catch(Exception e){}
}
}
CODIGO PARA LEER UN ARCHIVO BINARIO
import java.io.*;//nunca olvidemos importar las librerias necesarias
public class leer_binario {
public static void main(String args[]){
//el manejo de archivos siempre debe ir dentro de un try y catch
try{
FileInputStream fi = new FileInputStream("binary.ext");
ObjectInputStream is = new ObjectInputStream(fi);
/***************************************************************\
* como lo guardamos como objeto lo leemos asi y ademas como *
* sabemos que es un string lo casteamos a String y si fuera *
* de otro tipo en vez castearlo a String lo casteamos al *
* tipo que fuera.
*
\***************************************************************/
String texto = (String)is.readObject();
is.close();
fi.close();
System.out.println(texto);
}catch(Exception e){}
}
}
CODIGO EN VISUAL C# PARA CREAR UN ARCHIVO TXT
/*---------------------------*/
/* Ejemplo en C# nº 70
*/
/* ejemplo70.cs
*/
/*
*/
/* Escritura en un fichero */
/* de texto
*/
/*---------------------------*/
using System;
using System.IO;
// Para StreamWriter
public class Ejemplo70
{
public static void Main()
{
StreamWriter fichero;
fichero = File.CreateText("prueba.txt");
fichero.WriteLine("Esto es una línea");
fichero.Write("Esto es otra");
fichero.WriteLine(" y esto es continuación de la anterior");
fichero.Close();
}
}
CODIGO PARA LEER UN PROGRAMA EN C#
/* texto
*/
/*
*/
/* Introduccion a C#,
*/
/*
Nacho Cabanes
*/
/*---------------------------*/
using System;
using System.IO;
// Para StreamReader
public class Ejemplo71
{
public static void Main()
{
StreamReader fichero;
string linea;
fichero = File.OpenText("prueba.txt");
linea = fichero.ReadLine();
Console.WriteLine(linea);
Console.WriteLine(fichero.ReadLine());
fichero.Close();
Console.Read();
}
}
CODIGO EN VISUAL C# PARA LEER UN ARCHIVO TXT
/*---------------------------*/
/* Ejemplo en C# nº 72:
*/
/* ejemplo72.cs
*/
/*
*/
/* Lectura de un fichero de */
/* texto
*/
/*
*/
/*---------------------------*/
using System;
using System.IO;
// Para StreamReader
public class Ejemplo72
{
public static void Main()
{
StreamReader fichero;
string linea;
fichero = File.OpenText("prueba.txt");
do
{
linea = fichero.ReadLine();
if (linea != null)
Console.WriteLine(linea);
}
while (linea != null);
fichero.Close();
Console.Read();
}
}