Problems with PrintOutputStream

Status
Nicht offen für weitere Antworten.
R

Rosflurg

Gast
i have a question

when i write my streams to a file with a buffer, i only get the last part of the stream.
this is not my problem because the part of the stream i need is in the file

but now i want to start reading the stream and start writing after a number of bytes or after a certain word.

is there some trick to do this??

thanks,

Rosflurg
 
B

Beni

Gast
That's not the normal behaviour of streams. Please show your code, so we can reproduze the error.

Do you make more than one stream? Every new stream will delete the contents of the older streams.
 
R

Rosflurg

Gast
Code:
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;


class Link {

	public void GoSite() {
		String net = JOptionPane.showInputDialog(null,"Netnummer?");
		int start = Integer.parseInt(JOptionPane.showInputDialog(null,"Minimum?"));
		int end = Integer.parseInt(JOptionPane.showInputDialog(null,"Maximum?"));
		while(start<end) {
			String weblink = "http://www.zoekenbel.nl/telefoon.asp?zoek=nummer&tel_id=3061005&save=1&Type_invul=&kengetal="+net+"&Telnr="+start+"&#Zoek";
			GetInfo(weblink, net, start);
			start = start+1;
			
		}
	}
	
	public void GetInfo(String info, String begin,int nr) {
		FileOutputStream out;
		PrintStream p;
				
		try { 
        	URLConnection urlConn = new URL(info).openConnection(); 
        	urlConn.setUseCaches(false); 
        	InputStream in = urlConn.getInputStream(); 
          	byte buf[] = new byte[2048]; 
          	int nSize = in.read(buf); 
          	while(nSize>=0) { 
            	System.out.print(new String(buf,0,nSize));
            	String file = begin+nr+".txt";
               	try
                {
                        out = new FileOutputStream(file);
                        p = new PrintStream( out );
                        p.println (new String(buf,0,nSize));
                        p.close();
                }
                catch (Exception e)
                {
                        System.err.println ("Error writing to file");
                }
               	nSize = in.read(buf); 
          	}
        } 
      	catch(Exception e) 
      	{ 
        	System.out.println("Exception: "+e.getMessage()); 
      	} 
    }		
	
	public static void main(String[] args) {
		Link link = new Link();
		link.GoSite();
	}
}

this is my source-code
 
B

Beni

Gast
Use "new FileOutputStream( file, true );" instead of "new FileOutputStream( file );".
The "true" tells the stream, to append the new data, and not to override the old.

See FileOutputStream
 
R

Rosflurg

Gast
thanks for the answer, if works perfectly now.

but is it also possible to tell the FileOutputStream to start writing at say byte 1600 and end at byte 4800?

because my stream contains a lot of unnecessairy info.
 
B

bygones

Gast
you can use the RandomAccessFile class, which provides several methods to manipulate the start position of your writing
 
R

Rosflurg

Gast
Code:
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;


class Link {

	public void GoSite() {
		String net = JOptionPane.showInputDialog(null,"Netnummer?");
		int start = Integer.parseInt(JOptionPane.showInputDialog(null,"Minimum?"));
		int end = Integer.parseInt(JOptionPane.showInputDialog(null,"Maximum?"));
		while(start<end) {
			String weblink = "http://www.zoekenbel.nl/telefoon.asp?zoek=nummer&tel_id=3061005&save=1&Type_invul=&kengetal="+net+"&Telnr="+start+"&#Zoek";
			GetInfo(weblink, net);
			start = start+1;
			
		}
	}
	
	public void GetInfo(String info, String begin) {
		FileOutputStream out;
		PrintStream p;
		RandomAccessFile q;
		File name;
				
		try { 
        	URLConnection urlConn = new URL(info).openConnection(); 
        	urlConn.setUseCaches(false); 
        	InputStream in = urlConn.getInputStream(); 
          	byte buf[] = new byte[4096]; 
          	int nSize = in.read(buf); 
          	while(nSize>=0) { 
            	System.out.print(new String(buf,0,nSize));
            	String file = begin+".txt";
            	name = new File(file);
            	try
                {
                        out = new FileOutputStream(file, true);
                        p = new PrintStream( out );
                        q = new RandomAccessFile(name, new String(buf,0,nSize));
                        q.write(buf, 1400, 30);
                        p.close();
                }
                catch (Exception e)
                {
                        System.err.println ("Error writing to file");
                }
               	nSize = in.read(buf); 
          	}
        } 
      	catch(Exception e) 
      	{ 
        	System.out.println("Exception: "+e.getMessage()); 
      	} 
    }		
	
	public static void main(String[] args) {
		Link link = new Link();
		link.GoSite();
	}
}

okay my code looks like this now.

when i compile the file everything is okay, but when i run it
it can't write to the file.

any suggestions?
 

Illuvatar

Top Contributor
What do you mean - you can't write? Is there any Exception or is the file just empty afterwards or what?
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben