//Description:you can copy the files specified extension from one folder to another as a text files or same extension.
//Code:
import java .io.*;
import java.util.*;
class Ext implements FilenameFilter
{
String ext;
public Ext(String ext)
{
this.ext="."+ext;
}
public boolean accept(File dir,String name)
{
return name.endsWith(ext);
}
}
class InputFileDeclared {
FileInputStream in;
public InputFileDeclared(String filename) throws FileNotFoundException {
in = new FileInputStream(filename);
}
public String getWord() throws IOException {
int c;
StringBuffer buf = new StringBuffer();
do {
c = in.read();
buf.append((char) c);
} while (c != -1);
return buf.toString();
}
}
public class FileReadWrite
{
public static void main(String as[]) throws IOException
{
//enter the path here
String srcDir="C:/bibu/finalcut2/";
String desDir="C:/bibu/outf/";
File f1=new File(srcDir);
FilenameFilter only = new Ext("doc");// file extension
String s[]=f1.list(only);
String de[]=f1.list(only);
System.out.println(s.length);
for(int i=0;i<s.length;i++)
{
try
{
s=srcDir+s;
System.out.println(s);
InputFileDeclared file = new InputFileDeclared(s);
String show=file.getWord();
String cpydst=(desDir+de);
FileWriter out = new FileWriter(cpydst);
System.out.println(cpydst);
out.write(show);
show=null;
out.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
this s the code i m doing modification.. i m trying out deeply, because i m beginner, if u don mine do this for me plzchange this code to read first line only&write into only one document...>
import java.util.Scanner;
import java.io.*;
class NamedFileInOut
{
public static void main (String[] args) throws IOException
{
int num, square;
// Scanner for user input
Scanner user = new Scanner( System.in );
String inputFileName, outputFileName;
// prepare the input file
System.out.print("Input File Name: ");
inputfileName = user.nextLine().trim();
File input = new File( fileName );
Scanner scan = new Scanner( input );
// prepare the output file
System.out.print("Output File Name: ");
outputfileName = user.nextLine().trim();
File output = new File( fileName );
PrintStream print = new PrintStream( output );
// processing loop
while( scan.hasNextInt() )
{
num = scan.nextInt();
square = num * num ;
print.println("The square of " + num + " is " + square);
}
// close the output file
print.close();
}
}
now you can do whatever you want to just a demo. instead of using numbers try to break it in string array and read line and then output
> yes thank u... but i need to write code for moving
> first line from all document in specified
> location(nearly 45 to 60)documents)...to a single
> document
1) Open each document using Word.
2) Open a new Word document using Word.
3) Copy the first line of each of the Word documents opened in 1) to the new Word document opened in 2).
4) Close all Word documents.
Should take about 15 minutes. Much quicker than trying to program it in Java. Much quicker than asking for code in a Java forum.
no compilation error in this,, but it does not supporting MSWord file... but i need to read content from MS word only? could u plz correct this..? Another problem..(while running this program ,i can get only the last file content.. ...but i need all file content in destinatin file...)
//Description: you can copy the files specified extension from one folder to another as a text files or same extension.(reading first line)
//Code:
import java .io.*;
import java.util.*;
//import java.io.BufferedReader;
import java.io.FileReader;
class Ext implements FilenameFilter
{
String ext;
public Ext(String ext)
{
this.ext="."+ext;
}
public boolean accept(File dir,String name)
{
return name.endsWith(ext);
}
}
/* class InputFileDeclared {
FileInputStream in;
public InputFileDeclared(String filename) throws FileNotFoundException {
in = new FileInputStream(filename);
}
public String getWord() throws IOException {
int c;
StringBuffer buf = new StringBuffer();
do {
c = in.read();
buf.append((char) c);
} while (c != -1);
return buf.toString();
}
} */
public class ajosh2
{
public static void main(String as[]) throws IOException
{
String srcDir="C:/josh/test/";
String desDir="F:/merge.doc";
File f1=new File(srcDir);
FilenameFilter only = new Ext("doc");//file extention
String s[]=f1.list(only);
String de[]=f1.list(only);
File f2=new File(desDir);
System.out.println(s.length);
for(int i=0;i<s.length;i++)
{
try
{
s=srcDir+s;
System.out.println(s);
BufferedWriter out = new BufferedWriter(new FileWriter(f2));
// InputFileDeclared file = new InputFileDeclared(s);
BufferedReader in = new BufferedReader(new FileReader(s));
//String show=file.getWord();
//String cpydst=(f2+de);
//File f2=new File(desDir);
//FileWriter out = new FileWriter(f2);//instead cpydst
// System.out.println(cpydst);
out.write(in.readLine()+"\n");
in.close();
//show = null;
out.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}>