So here's a bit about the details.
I need to read the contents of file A, and B and store it in file C by joining the contents of A and B and also counting the number of letters/characters present in it.
I've come up with this so far.
Can someone help me here.
Any input would be great.

import java.io.FileInputstream; 
import java.io.FileNotFoundException;
import java.io.IOException;
public class JavaApplication43{
public static putwrite(string fname) throws FileNotFoundException, IOException 
{ FileInputStream m=newFileInput stream(fname), FileOutput stream o=newFileInputStream("D:\\hello.txt", true);
System.Out.println(""m.available());
int count=0,read=0;byte[] b=newbyte[100];
while((read =m.read(b))!=-1)
{if(read<b.length)
o.write(b,op.lenght);
else
o.write(b);
count+=read;}
return count;}
public static void main(string[]args)throws FileNotFoundException,IOException
{int abc=write("D:\\kolekar.txt");
ab=abc+write("D:\\man.txt");
System.out.println(abc);
}
}
Member Avatar for iamthwee
  1. Make sure you format and indent your code so that it is readable.
  2. Describe your error messages and what you are expecting.

I'm getting an overstack/flow error.
I am expected to contcatinate the contents of two files and place it in third and output total characters in third file.

Member Avatar for iamthwee

Don't use bytes, read and write the file using the buffered reader and buffered writer class respectively.

Once you've done that you can use a letter frequency counter to count the letters.

So like iamthwee said work on your formatting, I didnt even read your code. But anyway, I am a bit lazy so heres the code it ought to work as long as your file names are a b and c. If any dosent make sense ask, its important to understand the code youre using

public static void main(String[]args)
    {
        String a="", b="";
        try
        {
            Scanner file1 = new Scanner(new File ("a"));
            Scanner file2 = new Scanner(new File ("b"));
            while (file1.hasNext())
            {
                a+=file1.nextLine();
            }
            while (file2.hasNext())
            {
                b+=file2.nextLine();
            }
            file1.close();
            file2.close();
        }
        catch (FileNotFoundException fnfe)
        {
            System.out.println("File Not Found");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        try
        {
            FileOutputStream fos = new FileOutputStream("c", true);
            PrintWriter pw = new PrintWriter (fos);
            pw.println(a);
            pw.println(b);
            pw.close();
        }
        catch (FileNotFoundException fnfe)
        {
            System.out.println("Unable to find text file");
        }

        System.out.println("Length of file a: "+a.length()+"\nLength of file b: "+b.length()+"\nLength of file c: "+(a.length()+b.length()));
    }

You may not want to use that code for a number of reasons including:
Copying someone else's code for your homework is cheating
It corrupts the data by discarding the newline delimiters
It's a memory hog if the files are large (uses double the file size)
It does horribly inefficient string concatenations
It returns the file size in bytes, which may not be the number of characters, depending on the default encoding, and includes newline delimiters.

The original code is a far better starting place, once all its silly source errors are fixed (capitalisation of "string" or "Out", missing return type etc). It's hard to understand how you can get a stack overflow error with code that can't be compiled.

I fixed the code and came up with another solution to my project.
I used io-commons.jar from apache library to get the neccessary output.

public final class Concat {
    public static void main (String[] args) {
        Scanner inA = new Scanner(new File(args[0]));
        Scanner inB = new Scanner(new File(args[1]));
        PrintWriter out = new PrintWriter(new File(args[2]));

        int count = 0;
        while(inA.hasNextLine()) {
            String line = inA.nextLine();
            count += line.length();
            out.println(line);
        }

        while(inB.hasNextLine()) {
            String line = inB.nextLine();
            count += line.length();
            out.println(line);
        }

        out.println("Size:\t" + count);
        out.close();
        inA.close();
        inB.close();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.