Create a file if another file exist

Posted on 2004-11-18
Last Modified: 2010-04-01
I need to create files from a page. I know how to create it, but every request needs to have its own file with a progressive number at the final. Like the following:

if (fileN exist)
create fileN+1

I use the next code to create the file:

File f = new File("/Program Files/Apache Group/Tomcat 4.1/webapps/ROOT/New_Providers.txt");
PrintWriter out1 = new PrintWriter(new FileWriter(f));
Question by:mmartha
    LVL 3

    Expert Comment

    use this method to generate seqno...append this seq no to ur filename for every request
    for example "urfilename"+getSequenceNo("REQ");

          int getSequenceNo(String seqName) throws SQLException, NamingException {
                String newLeadNoSQL = "select " + seqName + ".nextval from dual";

                Connection conn = null;
                Statement stmt = null;
                ResultSet rs = null;

                int seqNo = 0;
                logger.debug("getSequenceNo query :" + newLeadNoSQL);

                try {
                      conn = DBUtils.getInstance().getConnection();
                      stmt = conn.createStatement();

                      rs = stmt.getResultSet();

                      if (
                            seqNo = rs.getInt(1);
                            throw new SQLException("Cant get next seq no from " + seqName);

                } catch (SQLException e) {
                      throw e;
                } finally {
                      try {
                            if (rs != null)
                      } catch (Exception e) {
                      try {
                            if (stmt != null)
                      } catch (Exception e) {
                      try {
                            if (conn != null)
                      } catch (Exception e) {
                return seqNo;
    LVL 19

    Accepted Solution

    Above code will require for you to create a Database and store the number there.. I think you want to work it out with files only..

    You should create a file in your System say
    initially it should have following line
    then in jsp put following code..

    fileProperties = new Properties();
    fileProperties.load(new FileInputStream("/Program Files/Apache Group/Tomcat 4.1/webapps/")));

    String  number = fileProperties.getProperty("NextNumber");

    Now you can use this number to append in your file names..

    After you have created your files you shd have following code..
    int nextNum = new Integer(number).intValue()+1;

    This should work for you
    LVL 2

    Author Comment

    do i need a special library for the "new Properties()" argument
    LVL 3

    Expert Comment

    just use the  util package
    LVL 3

    Expert Comment

    This is also one way to do...using properties to read from a config file..

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    OfficeMate Freezes on login or does not load after login credentials are input.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now