Create a file if another file exist

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:

request
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));
LVL 2
mmarthaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vikraman_bCommented:
Hi,
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();

                  stmt.execute(newLeadNoSQL);
                  rs = stmt.getResultSet();

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

            } catch (SQLException e) {
                  throw e;
            } finally {
                  try {
                        if (rs != null)
                              rs.close();
                  } catch (Exception e) {
                  }
                  try {
                        if (stmt != null)
                              stmt.close();
                  } catch (Exception e) {
                  }
                  try {
                        if (conn != null)
                              conn.close();
                  } catch (Exception e) {
                  }
            }
            return seqNo;
      }
0
KuldeepchaturvediCommented:
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 number.properties
initially it should have following line
NextNumber=1
then in jsp put following code..

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

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;
fileProperties.setProperty("nextNumber",nextNum);
fileProperties.save();


This should work for you
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mmarthaAuthor Commented:
do i need a special library for the "new Properties()" argument
0
vikraman_bCommented:
just use the  util package
java.util.Properties
0
vikraman_bCommented:
This is also one way to do...using properties to read from a config file..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.