FANDOM


TINF-Maturafragen

Stream-Beispiel 1Bearbeiten

import java.io.*;

public class streamsbsp1
{
	public static void main(String[] args)throws IOException
{ FileReader f; int input=0,a=0,e=0,i=0,o=0,u=0; char t; f = new FileReader("buffer.txt"); while((input = f.read())!=-1) { switch(input) { case 'a': a++; break; case 'e': e++; break; case 'i': i++; break; case 'o': o++; break; case 'u': u++; break; } } System.out.println("a:"+a+"| e:"+e+"| i:"+i+"| o:"+o+"| u:"+u); f.close(); } }


Stream-Beispiel 2Bearbeiten

import java.io.*;

public class Streambsp2
{
	public static void main(String[] args) throws FileNotFoundException,IOException
	{
		BufferedReader fileIn;
		BufferedWriter fileOut;
		String str;
		
		fileIn = new BufferedReader(new FileReader("buffer.txt"));
		fileOut = new BufferedWriter(new FileWriter("output.txt"));
		
		for(int zNr=1;(str=fileIn.readLine())!=null;zNr++)
		{
			str = zNr + ".:"+str;
			fileOut.write(str,0,str.length());
			fileOut.newLine();
		}
		fileIn.close();
		fileOut.close();
	}
}

Stream-Beispiel 3Bearbeiten

import java.io.*;

public class Streambsp3
{
	public static void main(String[] args) throws FileNotFoundException,IOException
	{
		BufferedInputStream fileIn;
		BufferedOutputStream fileOut;
		
		int puffersize = 100; 	//größe des puffer arrays
		int anz = 0;			//anzahl der lesbaren zeichen werden in anz gespeichert!
		
		byte[] puffer = new byte[puffersize];
		
		fileIn = new BufferedInputStream(new FileInputStream("buffer.txt"));
		fileOut = new BufferedOutputStream(new FileOutputStream("checkit.txt"));
		
		while((anz=fileIn.read(puffer,0,puffersize))!=-1)	// !=-1 -> abbrechen falls nichts mehr gelesen
			fileOut.write(puffer,0,anz);//nur soviele zeichen schreiben, wie tatsächlich gelesen!
			
		fileOut.close();
		fileIn.close();
	}
}