• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

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));
0
mmartha
Asked:
mmartha
  • 3
1 Solution
 
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
 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now