How to use BufferedInputStream: mark() method in java code?

Below code shows how to use BufferedInputStream: mark() method in java –

/**********************************************************************************
 * Created on Nov, 2004 Copyright(c) http://kodehelp.com All Rights Reserved.
 **********************************************************************************/
package com.kodehelp.java.io;
 
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
/**
 * @author http://kodehelp.com
 *
 */
public class BufferedInputStreamMark {
 
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        BufferedInputStream inputStream = null;
        try{
            inputStream = new BufferedInputStream(new FileInputStream("/TestFile.txt"));
            System.out.println("First Character in Buffer ::"+(char)inputStream.read());
            inputStream.mark(0);
            System.out.println("Next Character in buffer is  : "+(char)inputStream.read());
            System.out.println("Next Character in buffer is  : "+(char)inputStream.read());
            System.out.println("Next Character in buffer is  : "+(char)inputStream.read());
 
            System.out.println("reset method called");
 
            inputStream.reset();
 
            System.out.println("Next Character in buffer is  : "+(char)inputStream.read());
            System.out.println("Next Character in buffer is  : "+(char)inputStream.read());
 
        }catch(FileNotFoundException e){
            System.out.println("File not found" + e);
        }catch(IOException ioe){
            System.out.println("Exception while reading the file " + ioe);
        }finally{
            //close the BufferedInputStream using close method
            try{
                if(inputStream != null)
                    inputStream.close();
 
            }catch(IOException ioe){
                System.out.println("Error while closing the stream : " + ioe);
            }
        }
    }
 
}

8 comments

  1. Ankit1685 says:

    Hi DeeKay,
    Your code “java program for uploading file to sftp server ” is very good and working fine but can you please give the code to upload directory.

  2. Ankit1685 says:

    Hi DeeKay,
    Your code “java program for uploading file to sftp server ” is very good and working fine but can you please give the code to upload directory.

  3. Umeshkumar says:

    Thank you very much for the code for uploading the file into sftp.

    I was trying to do this in a standalone application, which is not working for me and giving this error. I tried a lot for this and i am not able to resolve this :

    com.jcraft.jsch.JSchException: Auth fail
    at com.jcraft.jsch.Session.connect(Session.java:461)
    at com.jcraft.jsch.Session.connect(Session.java:154)
    at com.src.ftp.Sftp2.moveFileToDir(Sftp2.java:74)
    at com.src.ftp.Sftp2.moveFileToDir(Sftp2.java:29)
    at com.src.ftp.ConnectToUpload.main(ConnectToUpload.java:86)

    My code :

    I know to find the real error it is required to see the code. But it is the same core written here. The difference is the server name, username, password.

    I tried to connect to this server with wnscp which is working fine and the servername, username, password and the port are working fine.

    I am able to paste the file from my machine to this server also.

    The directory structure where i need to upload the file is already there.

    Can you please help to resolve this error ?

    thanks in advance

    • admin says:

      @Umesh – Are you trying to make SFTP Connection ? You said you were able to make connection through WinSCP. Can you please let me know which Port you used to make connection. If you are making FTP connection then you have to use Port 21. If you are trying SFTP connection then the port number for your SFTP server will be different than Port 22. Please make sure you have exact port number to make connection.

      Let me know if this still don’t work. We can have 1 on 1 discussion on this through skype.

      Regards
      Deekay
      Vigilance Admin

  4. admin says:

    @Umesh – Are you trying to make SFTP Connection ? You said you were able to make connection through WinSCP. Can you please let me know which Port you used to make connection. If you are making FTP connection then you have to use Port 21. If you are trying SFTP connection then the port number for your SFTP server will be different than Port 22. Please make sure you have exact port number to make connection.

    Let me know if this still don’t work. We can have 1 on 1 discussion on this through skype.

    Regards
    Deekay
    Vigilance Admin

  5. gubbaraviteja says:

    com.jcraft.jsch.JSchException: connection is closed by foreign host
    at com.jcraft.jsch.Session.connect(Session.java:248)
    at com.jcraft.jsch.Session.connect(Session.java:162)
    at com.tcs.telecom.tib.CLM.sftp.main(sftp.java:38)
     
    I got this error while trying to execute the code

    • admin says:

      It looks like a socket timeout. Can you connect to the server with command line ssh/sftp from the same machine ?

  6. Von_casa says:

    I still do not understand why people cannot do something like this. Simple, elegant, working.

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.