Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Jallal
I/O Java
2 Answer(s)      2 years and a month ago
Posted in : Java Beginners


import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class Concat {

public boolean accept(File file) {
    //if the file extension is .txt or .java return true, else false
    if (file.getName().endsWith(".txt")||file.getName().endsWith(".java")) {
        return true;
    }
    return false;
}

public void copy(PrintWriter pw, String inputFile)
{
    File f = new File(inputFile);
    if(accept(f)==true){

        try {
            Scanner scan = new Scanner(f);
            pw= new PrintWriter(new FileOutputStream("Stuff.txt"));
            while (scan.hasNextLine())
            {
                String s = scan.nextLine();
                pw.println(s);
            }
            scan.close();
        } catch (FileNotFoundException e) {
            System.out.println(" Error: "+e + " "+ inputFile);
        }
    }else{

        System.out.println("File name must be a TEXT Or JAVA type");

    }
}

// The routine will throw a RuntimeException if the inputFile name does 
// note end with .java or .txt

// This routine will also throw a RuntimeException if any problem occurs in 
// opening the inputFile

// The copy routine copies the contents of the inputFile into the PrintWriter


public void concat(String outFile, String[] inputFiles)
{
    try {
        PrintWriter writer = new PrintWriter(outFile);
        for(int i=0;i<inputFiles.length;i++){
            String source=inputFiles[i];
            copy(writer,source);
        }

    } catch (IOException e) 
    {
        System.err.println(e);
        System.exit(1);
    }

    // Fill in details.
    // The routine will call copy to do a copy of a single file
    // This routine will also create an open PrintWriter for the outFile
    // If any problem occurs, a RuntimeException should be thrown with 
    // appropriate error information
}


public static void main(String[] args) {
    if (args == null || args.length < 2)
    {
        System.out.println("Concat needs at least 2 filenames");
        return;
    }
    try
    {

        String[] inputFiles = new String[args.length-1];
        for (int i=0; i < inputFiles.length; i++)
            inputFiles[i] = args[i+1];
        Concat c = new Concat();
        c.concat(args[0], inputFiles);
    }
    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);
    }
}

}


Advertisement
View Answers

September 12, 2012 at 1:45 AM


I am not really sure why This program is not running properly? Any ideas? Thanks in advance.



September 13, 2012 at 1:02 PM


Here is an example of concatenating text files.

import java.io.*;
import java.util.*;

public class Concat {

        static public void main(String arg[]) throws java.io.IOException {
                PrintWriter pw = new PrintWriter(new FileOutputStream("c:/concat.txt"));
                Scanner input=new Scanner(System.in);
                System.out.print("How many files you want to concat?:");
                int no=input.nextInt();
                String f[]=new String[no];
                System.out.println("Enter filenames: ");
                for(int i=0;i<f.length;i++){
                    String filename=input.next();
                    int mid= filename.lastIndexOf(".");
                    String ext=filename.substring(mid+1,filename.length());
                    if(ext.equals("txt")){
                    f[i]=filename;
                      System.out.println("Processing " + f[i] + "... ");
                        BufferedReader br = new BufferedReader(new FileReader(f[i]));
                        String line = br.readLine();
                        while (line != null) {
                                pw.println(line);
                                line = br.readLine();
                        }
                        br.close();
                    }
                    else{
                        System.out.print("Only txt files should be concated.Re-enter file name: ");
                    }
                }
                 pw.close();
                System.out.println("All files have been concatenated into concat.txt");
        }
}


Related Tutorials/Questions & Answers:
Java i/o
Java i/o   How can you improve Java I/O performance
Java I/O
Java I/O   What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy
Advertisements
Java I\O file
Java I\O file  What is the difference between the File and RandomAccessFile classes
Java I/O stream
Java I/O stream  What class allows you to read objects directly from a stream
java i/o operations
java i/o operations  how to write integer data to a file in java using random access file object or file writer or data outputstream i have already tried the write and writeInt methods....plz help
Java i/o opearations
Java i/o opearations  "FOLLOWING IS MY LAST QUESTION ON JAVA I/O... to a file in java using random access file object or file writer or data outputstream i..."),true)); for(int i=1;i<=10;i++){ System.out.print("Enter Number
Java I/O problem
Java I/O problem   Write a Java application that prompts the user to input their Name, Address, Date of Birth and Student ID number using the standard input - this information should then be saved to a file named studentData
I/O Java
I/O Java  import java.io.File; import java.io.FileNotFoundException...(File file) { //if the file extension is .txt or .java return true, else false if (file.getName().endsWith(".txt")||file.getName().endsWith(".java
Java I/O
Java I/O  What value does readLine() return when it has reached the end of a file
Java I/O
Java I/O  how to write different type of arrays like string,int,double etc into a file using DataOutputStream
Java I/O - Java Beginners
Creating Directory Java I/O  Hi, I wanted to know how to create a directory in Java I/O?  Hi, Creating directory with the help of Java Program is not that difficult, go through the given link for Java Example Codehttp
Java I/O Assistance + Loop
Java I/O Assistance + Loop  I'm trying to make this program write... and append all odd numbers 1-100 and finally close file. But for some reason I CANNOT Get the output as I want. I know where the error lays I just can't seem
java i/o - Java Beginners
java i/o  thnx alot sir that this code helped me much in my program... so that i could write it line by line such as- Hello Java in roseindia Hello... Java in roseindia Hello Java in roseindia Hello Java in roseindia Hello Java
java i/o - Java Beginners
java i/o  Dear sir, i wrote a program where program asks "Enter your... gets closed. when i open the program again and enter text in this case previous texts get replaced with new text. i tried my best but got failed plz tell me
File I/O - Java Beginners
File I/O  How to search for a specific word in a text file in java?  Hi Arnab, Please check the following code. ===================== import java.io.File; import java.io.BufferedReader; import
File I/O - Java Beginners
File I/O  Suppose a text file has 10 lines.How do I retrieve the first word of each line in java? abc fd ds 10 fg yy6 uf ui . . . . . . yt oi oiu 25 ewr ytro 9+ po I want to retrieve 'abc' 'fg' .... 'yt' 'ewr
Java I/O
JAVA I/O Introduction The Java Input/Output (I/O) is a part of java.io package. The java.io package contains a relatively large number of classes....  For more Details click the following links below : Java I/0 Complete
Java I/O Data Streams
Java I/O Data Streams In this tutorial we will discuss the Java I/O Data Streams. To deal with the binary I/O of primitive data type values as well as the String values Java provided the support of Data stream. DataInput
Text File I/O - DVD.java - Java Beginners
Text File I/O - DVD.java  NEED HELP PLEASE. The application should... the array to recreate the file. Here is my code but I need to modify it to the specifications on top. I don't know where to begin. Thanks. //package dvd
Java I/O Examples
; What is Java I/O? The Java I/O means... In this section we will discussed about How files can be handled in Java. Java I/O Byte Streams In this section we will discussed the I/O Byte Streams. Java
Java I/O From the Command Line
Java I/O From the Command Line In this section we will learn about the I/O from the command line in Java. Several times you can see that the programs runs after taking the input from the command line. Java also supports for the command
Java I/O Character Streams
Java I/O Character Streams In this section we will discuss the I/O Character... and for this Java provides the Character stream I/O. Character stream I/O... the character stream I/O the character stream classes are defined. These classes
Java I/O Byte Streams
Java I/O Byte Streams In this section we will discussed the I/O Byte Streams... are used where the program needs to work with the raw binary data. In Java to handle I/O raw binary data the byte stream classes are defined. For all of the byte
Java I/O Buffered Streams
Java I/O Buffered Streams In this section we will discuss the I/O Buffered Streams. In Java programming when we are doing an input and output operation then we are trying to interact our Java program to read from or write data
i/o
i/o  java program using inputstream and outputstream   Hi Friend, Try the following code: import java.io.*; class InputStreamAndOutputStream { public static void main(String[] args)throws
i/o
i/o  java program using inputstream and outputstream   Hi Friend, Try the following code: import java.io.*; class InputStreamAndOutputStream { public static void main(String[] args)throws Exception
i/o
i/o  java program using inputstream and outputstream   Hi Friend, Try the following code: import java.io.*; class InputStreamAndOutputStream { public static void main(String[] args)throws Exception
i/o
i/o  java program using inputstream and outputstream   Hi Friend, Try the following code: import java.io.*; class InputStreamAndOutputStream { public static void main(String[] args)throws Exception
i/o
i/o   Write a Java program to do the following: a. Write into file the following information regarding the marks for 10 students in a test i. Matric no ii. Marks for question 1 iii. Marks for question 2 iv. Marks
I/O stream class.
I/O stream class.  Explain the hierarchy of Java I/O stream class.   Hierarchy of Java I/O streams Have a look at the following link: Java I/O
file i/o - Java Beginners
file i/o  hiii, i have to read some integers from a text file and store them in link list.. so please can i have source code for it.?? thanks
 

 

 

DMCA.com