?
Solved

Multiple FileInputStreams

Posted on 2005-03-23
10
Medium Priority
?
283 Views
Last Modified: 2012-05-05
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
Comment
Question by:Squeeto
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13618934
what process is it you are waiting for?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13618981
If you invoke external process via a Runtime exec method then you can call waitFor on that Process
0
 

Author Comment

by:Squeeto
ID: 13619004
   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!

 

Author Comment

by:Squeeto
ID: 13619069
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
 
LVL 5

Expert Comment

by:Triguna
ID: 13619218
Use Thread.sleep(1000). or whatever time it will take to read.
0
 

Author Comment

by:Squeeto
ID: 13621768
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
 
LVL 5

Accepted Solution

by:
Triguna earned 375 total points
ID: 13621905
Have tried to use a class which extends Thread and

in that class's run method, write Thread.sleep(1000);
0
 
LVL 6

Expert Comment

by:guitaristx
ID: 13624115
0
 

Author Comment

by:Squeeto
ID: 13630958
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
 
LVL 5

Expert Comment

by:Triguna
ID: 13634792
Thanks for the points.

happy coding.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 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