Results 1 to 4 of 4
  1. #1
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Need help with writing a code which will check if a file is a .txt file

    Hello, I'm having trouble coding a "check for" substring. I need to make a code which reads the filename that its trying to open if its either a .txt file or a .java file then it runs normally, but if it is anything else then it throws a RuntimeException. Could somebody write a suedo code for me or point me in the right direction in creating this "check for."

    I'm running this program with arguments in Eclipse.

    Thank you.

    Here's the body of code I'm having trouble with

    Java Code:
        else if (!outFile.substring(outFile.charAt('.')).equals(condition))
                    {
                        throw new RuntimeException("Bad Suffix");
                    }
    Here's the full body of code.

    Java Code:
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.PrintStream;
    import java.util.Scanner;
    
    public class Concat{
    
        public void copy(InputStream is, PrintStream ps)
        {
            Scanner scan = new Scanner(is);
            while (scan.hasNextLine())
            {
                String s = scan.nextLine();
                ps.println(s);
            }
            scan.close();
        }
    
    
        void concat(String[] inFiles,  int numInputs, String outFile)
        {
    
            String condition = "txt";
    
            try {
                FileOutputStream fo = new FileOutputStream(outFile);
                PrintStream ps = new PrintStream(fo);
                for (int i=0; i < numInputs; i++)
                {
                    if(outFile == null)
                    {
                        FileInputStream fi = new FileInputStream(inFiles[i]);
                        copy(fi, System.out);
                    }
                    else if (!outFile.substring(outFile.charAt('.')).equals(condition))
                    {
                        throw new RuntimeException("Bad Surfix");
                    }
                    else    
                    {
                        try
                        {
                            FileInputStream fi = new FileInputStream(inFiles[i]);
                            copy(fi, ps);
                        }
                        catch (Exception e)
                        {
                            throw new RuntimeException("Unable to open" + e);
                        }    
                    } 
                }
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    
        }
    
    
        public void process(String[] args)
        {
            switch(args.length)
            {
            case 0:
                System.out.println("No input files, defaulting to reading from the keyboard and writing to the screen");
                concat(args, 0, null);
                break;
            case 1:
                System.out.println("No output file, defaulting to writing to the screen");
                concat(args,  1, null);
                break;
            default:
                concat(args,  args.length-1, args[args.length-1]);
                break;
            }   
        }
    
    
        public static void main(String[] args) {
    
            Concat concat = new Concat();
            try
            {
                concat.process(args);
            }
            catch (RuntimeException e)
            {
    
                // Possible errors:
                // An input file that doesn't end with .java or .txt
                // An input file that doesn't open properly.
                // Trouble creating the output file
    
                System.out.println(" Error in Concat:"+e);
            }
        }
    
    }

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default Re: Need help with writing a code which will check if a file is a .txt file

    Java Code:
    String filename = "filename.txt";
    if(filename.endsWith(".txt"))
    {
      ...
    }
    else if(filename.endsWidth(".java"))
    {
      ...
    }

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Need help with writing a code which will check if a file is a .txt file

    Put a toLowerCase() in there and it will actually work reliably too. On Windows, where file extensions actually mean something. Now try on a Mac where you are likely to get a file called 'filename' only. Then what?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: Need help with writing a code which will check if a file is a .txt file

    And a .dat? Could be binary data or textual data.

    Regards,
    Jim
    Last edited by jim829; 09-20-2013 at 03:40 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 0
    Last Post: 12-15-2012, 01:37 AM
  2. Replies: 5
    Last Post: 10-17-2012, 10:07 AM
  3. Replies: 1
    Last Post: 08-02-2012, 03:01 AM
  4. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  5. Replies: 3
    Last Post: 05-09-2009, 11:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •