answersLogoWhite

0


Best Answer

I suggest something like this:* Open file1 and file2

* Create a temporary output file, for writing

* Read a line from file1, write it into the output file

* Read a line from file2, write it into the output file

* Repeat the previous two steps, while you are not at end-of-file in either of the two files

* At this point, if you are NOT at end-of-file in file1, read the remaining lines of file1, and write them to the output (you can write a loop for this).

* Similarly, if you are NOT at end-of-file in file2, read and write the remaining file.

* Close file1, file2, and the output file.

* Copy the temporary output file back to file1

* Erase the temporary output file

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write java program to merge alternate lines from file1 and file2 into file1?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you create a File in Java?

There are a number of ways to create a file in java. The following example demonstrates a few: import java.io.*; import java.nio.charset.*; public class Main { public static void main(String[] args) { try { // Simplest way PrintWriter out1 = new PrintWriter("file1.txt"); out1.println("hello file1!"); out1.close(); // Append to existing file if it already exists PrintWriter out2 = new PrintWriter(new FileWriter("file2.txt", true)); out2.println("hello file2!"); out2.close(); // If non-default charset is needed Charset charset = Charset.forName("UTF-16"); PrintWriter out3 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("file3.txt"), charset)); out3.println("hello file3!"); out3.close(); } catch (IOException e) { e.printStackTrace(); } } }


What is the difference between global and extern?

AnswerGlobal variables and extern variables are very similar, but with a major difference. Let's say you have a global variable in your header file, like this:( Refer how u can add the variable declaration in the source code as extern, i think we cant add into header files as it said)int x = 0;Now, in each file that includes that header file, there will be a new variable called x. Modifying x in file1.cpp won't modify x in file2.cpp.Now, for externs... Say you have something like this in your header:extern int x;Then, in file1.cpp (which includes your header):int x = 0;Then, in file2.cpp (also includes the header):x = 46;x in file1.cpp is also now equal to 46. You see, when you use an extern variable, you're telling the compiler that you want to use that variable across all of your source files. If you use a regular global, however, you create a new variable in each of your source files.Anyway, that's just for variables, which I assume you're talking about - global and extern functions are a bit different, and I'm not entirely clear on them.


Write a c program to copy the contents from one file to another file?

#include #include int main() { FILE *fs1, *fs2, *ft; char ch, file1[20], file2[20], file3[20]; printf("Enter name of first file\n"); gets(file1); printf("Enter name of second file\n"); gets(file2); printf("Enter name of file which will store contents of two files\n"); gets(file3); fs1 = fopen(file1,"r"); fs2 = fopen(file2,"r"); if( fs1 == NULL fs2 == NULL ) { perror("Error "); printf("Press any key to exit...\n"); getch(); exit(EXIT_FAILURE); } ft = fopen(file3,"w"); if( ft == NULL ) { perror("Error "); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while( ( ch = fgetc(fs1) ) != EOF ) fputc(ch,ft); while( ( ch = fgetc(fs2) ) != EOF ) fputc(ch,ft); printf("Two files were merged into %s file successfully.\n",file3); fclose(fs1); fclose(fs2); fclose(ft); return 0; }


What is linkage in c plus plus?

"An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage"Types:- External, Internal and None linkageExample:-External Linkage= extern int a1;Internal Linkage= static int a2;None Linkage= int a3;


Which is the keyword used to access the variable declared as global variable in another file?

To be able to access a variable declared in one file/class from another file/class you need to declare that variable as "public". Doing this is a very bad coding practice because exposing a variable of a class to be available publicly to other classes is totally against the Java object oriented concepts of Encapsulation and Data hiding. So, preferably you shouldn't be doing this. However, a better way to do this is, declare the variable as private and have public accessor methods through which you can access this variable. This will ensure that your data is protected even if it is available outside the class

Related questions

Concatenate two files without using strcat function?

Win/Dos: copy file1+file2 tofile unix: cat file1 file2 >tofile


How do you combine multiple files into one file in Linux?

cat file1 file2 file3 > file4


What is UNIX commands for the compare files but stops at first occurrence of a difference?

cmp file1 file2


Write a shell script that copies multiple files to a directory?

why not use the cp command to copy multiple files, i.e. cp file1 file2 file3 dir Rick


How do you write a program to illustrate the concept of extern variable?

In order to use extern you have to have at least two files. In first one, let's call it file1.cpp, you will define a variable using extern (in this case belongs to int):...extern int myVar = 0;...Then in file2.cpp file where you have main() you need to write following:extern int myVar;Do not initialize the variable in file2.cpp, or you code will not compile.


Without using cp command you copy file to another file in unix?

The easiest way would be: cat file1 > file2


What Linux command is used to compare the difference between two file display the difference?

The command is: diff file1.txt file2.txt


How do you create a file using terminal in Linux?

You can use cat to create files by specifying an input for it and redirecting its output to a new file.cat file1 > file2That would make a copy of file1 and call it file2. To join two or more files, you can simply add more files for cat to read. cat file1 file2 file3 file4 > file5


Can the tee command make five copies of a file?

cat inputfile | tee file1 | tee file2 | tee file3 | tee file4 > file5


What does the unix command 'comm' stands for?

comm is stand for - finding what is common. The comm command reads two sorted files,file1,and file2,to produce a three-column output. The first column gives lines that exist only in file1; the second column displays lines that exist only in file2; and the third column shows the lines common in both files. //hop helpful thanks


What is the command to run a unix program?

Just type the name of the program. If you want to run the dump program, type dump. if you want to open the links web browser, type links. ..... Some programs take arguments, for example the cat program accepts one or more files as an argument, so you could call it by typing $ cat file1 [file2, file3...]


Is there a simple way to combine multiple text files into 1 large text file in Windows?

Open a Command Prompt window, go to the folder where the files are (or you can use whole path names), and enter: COPY /A file1.txt + file2.txt + file3.txt + ... + lastfile.txt destination.txt /A