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

Multiple FileInputStreams

I have an app. that attepts to do a couple FileInputStreams and FileOutputStreams.  For small files it works fine.  I believe larger files fail because of timing.  Is it possible to wait for processes to end before the next begins?
 try
  FileInputStream ..
  FileOutputStream ..
  ..read()
  ..write(data);
 catch
//let me know when the process ends and go on to the next
 try
  FileInputStream ..
 catch
//let me know when the process ends and go on to the next
 try
  FileOutputStream ..
 catch
0
Squeeto
Asked:
Squeeto
1 Solution
 
objectsCommented:
what process is it you are waiting for?
0
 
aozarovCommented:
If you invoke external process via a Runtime exec method then you can call waitFor on that Process
0
 
SqueetoAuthor Commented:
   private String getGif()
    {
      String s = "";
      JFrame f = new JFrame();
      FileDialog fd = new FileDialog(f,  "Open", FileDialog.LOAD);
      fd.setFile("*.gif");
      fd.setVisible(true);
      String zfile = fd.getFile();
      String zdir = fd.getDirectory();
      String zdirfile = fd.getDirectory() + zfile;
      if (zfile != null)
      {
        File dir = new File("");
        String wd = dir.getAbsolutePath()+ "\\progs\\";
        if (! zdir.equals(wd))
        {
          try
          {
            FileInputStream fis = new FileInputStream(zdirfile);
            FileOutputStream fos = new FileOutputStream(".\\progs\\" +zfile);
            while (true)
            {
              int data = fis.read();
              if (data < 0)
                break;
              fos.write(data);
            }
            fis.close();
            fos.close();
          }
          catch (IOException e) {}
        }
        String java = "class " + zfile.substring(0,zfile.length()-4)
                    + " {\npublic final static int[] IMAGE = {\n";
        try
        {
          FileInputStream fis = new FileInputStream(zdirfile);
          int count = 0;
          while (true)
          {
            int data = fis.read();
//'here'
            if (data < 0)
            {
              java = java.substring(0, java.length() - 1) + "\n};\n}";
              break;
            }
            if (count > 15)
            {
              java = java + "\n";
              count = 0;
            }
            count ++;
            java = java + data + ",";
          }
          fis.close();
        }
        catch (IOException e) {java = java + "\n};\n}";}
        try
        {
          FileOutputStream fos = new FileOutputStream(".\\progs\\"
                                 +zfile.substring(0,zfile.length()-3) +"java");
          byte zdata[] = java.getBytes();
          fos.write(zdata);
          fos.close();
        }
        catch (IOException e) {}
        s = zfile.substring(0,zfile.length()-4);
      }
      return s;
    }

This is it.  It seems to fail at 'here'.  data is less than zero and I get an empty resource file.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SqueetoAuthor Commented:
Just to mention again, this does work for small gifs.  I guess the second fis trys to read before the first fos is finished.
What this does: the user selects his gif, it is copied to a working directory and is copied into a .java resource file.
0
 
TrigunaCommented:
Use Thread.sleep(1000). or whatever time it will take to read.
0
 
SqueetoAuthor Commented:
Already tried:
  try {Thread.sleep(2000);} catch(Exception e) {}
Didn't fix the problem.  I not sure if Thread.sleep stops the fis and fos also and just postpones the bottle neck.
This is old code that used to work just fine on my old (very slow) computer.

For now, I think the fix is to remove the file copy part.  I don't really need this icing.
A simple file read, modify, write should work fine.

I will leave this open another day.
Squeeto
0
 
TrigunaCommented:
Have tried to use a class which extends Thread and

in that class's run method, write Thread.sleep(1000);
0
 
guitaristxCommented:
0
 
SqueetoAuthor Commented:
Thanks Triquna.
I use a
  class ThreadReader extends Thread
when I initiate my Runtimes.  It could work for file IO also.

I decided to simplify the code instead.  This works:

        String java = "class " + zfile.substring(0,zfile.length()-4)
                    + " {\npublic final static int[] IMAGE = {\n";
        try
        {
          FileOutputStream fos = new FileOutputStream(".\\progs\\"
                                 +zfile.substring(0,zfile.length()-3) +"java");
          fos.write(java.getBytes());
          FileInputStream fis = new FileInputStream(zdirfile);
          int count = 0;
          boolean once = true;
          while (true)
          {
            int data = fis.read();
            if (data < 0)
            {
              java = "\n};\n}";
              fos.write(java.getBytes());
              break;
            }
            if (count > 15)
            {
              java = "\n";
              fos.write(java.getBytes());
              count = 0;
            }
            count ++;
            if (once)
            {
              java = String.valueOf(data);
              once = false;
            }
            else
            {
              java = "," + data;
            }
            fos.write(java.getBytes());
          }
          fis.close();
          fos.close();
        }
        catch (IOException e) {}

0
 
TrigunaCommented:
Thanks for the points.

happy coding.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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