0% found this document useful (0 votes)
7 views8 pages

Java File Operations and String Methods

The document contains programming exercises for B.Sc. (I.T) students at Lyallpur Khalsa College, focusing on file operations and string manipulation in Java. It includes code examples for copying characters and bytes between files, sorting an array of strings, and demonstrating string methods. Each exercise is presented with a brief description and corresponding Java code implementation.

Uploaded by

Sunaina 0000
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Java File Operations and String Methods

The document contains programming exercises for B.Sc. (I.T) students at Lyallpur Khalsa College, focusing on file operations and string manipulation in Java. It includes code examples for copying characters and bytes between files, sorting an array of strings, and demonstrating string methods. Each exercise is presented with a brief description and corresponding Java code implementation.

Uploaded by

Sunaina 0000
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Department of Computer Science(I.

T)
Lyallpur KhalsaCollege,Jalandhar

LKC/[Link]. (I.T) IV-Sem./2017/10531


Department of Computer Science(I.T)
Lyallpur KhalsaCollege,Jalandhar

[Link] a program to copy characters from file named “[Link]” into a file called
“[Link]”

import [Link].*;
public class CopyFile {

public static void main(String args[]) throws IOException {


FileInputStream in = null;
FileOutputStream out = null;

try {
in = new FileInputStream("[Link]");
out = new FileOutputStream("[Link]");

int c;
while ((c = [Link]()) != -1) {
[Link](c);
}
}finally {
if (in != null) {
[Link]();
}
if (out != null) {
[Link]();
}
}
}
}

LKC/[Link]. (I.T) IV-Sem./2017/10531


Department of Computer Science(I.T)
Lyallpur KhalsaCollege,Jalandhar

LKC/[Link]. (I.T) IV-Sem./2017/10531


Department of Computer Science(I.T)
Lyallpur KhalsaCollege,Jalandhar

[Link] a program to copy bytes from one file to another.

import [Link].*;
class CopyBytes
{
public static void main(String[] args)
{
FileInputStream fis = null;
FileOutputStream fos = null;

try{
fis = new FileInputStream("[Link]");
fos = new FileOutputStream("[Link]");
int ch;
while((ch=[Link]())!=-1){
[Link](ch);
}
}catch(IOException e)
{
[Link](e);
[Link](-1);
}
finally{
try{
[Link]();
[Link]();
}
catch(IOException e){}
}
}}

LKC/[Link]. (I.T) IV-Sem./2017/10531


Department of Computer Science(I.T)
Lyallpur KhalsaCollege,Jalandhar

LKC/[Link]. (I.T) IV-Sem./2017/10531


Department of Computer Science(I.T)
Lyallpur KhalsaCollege,Jalandhar

[Link] a program to sort an array of strings in alphabetical order.

public class Ordering{


static String[] cities = {"Delhi", "Mumbai", "Agra", "Chennai", "Jalandhar", "Jabalpur"};
public static void main(String[] args) {
int n = [Link];
String temp = null;
[Link]("Citites before sort");
for (int i = 0; i < n; i++) {
[Link](cities[i]);
}
[Link]("\n===========\n");
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (cities[i].compareTo(cities[j]) > 0) {
temp = cities[i];
cities[i] = cities[j];
cities[j] = temp;
}
}
}
[Link]("Citites after sort");
for (int i = 0; i < n; i++) {
[Link](cities[i]);
}
}
}

LKC/[Link]. (I.T) IV-Sem./2017/10531


Department of Computer Science(I.T)
Lyallpur KhalsaCollege,Jalandhar

LKC/[Link]. (I.T) IV-Sem./2017/10531


Department of Computer Science(I.T)
Lyallpur KhalsaCollege,Jalandhar

[Link] a program to demonstrate the use of some string methods that are used for
manipulating strings.

public class String2{


public static void main(String args[]) {
String text = "hellothere";
int index = [Link](5);
int indexOf = [Link]('e');
[Link]("Length of " + text + " is "+[Link]());
[Link]("ASCII value : " + index);
[Link]("\nFirst index of 'e' : " + indexOf);
[Link]("Sub String:"+[Link](2,5));
for(int i = 0; i < [Link](); i++){
[Link]("Char at " + i + " is " + [Link](i));
}
}
}

LKC/[Link]. (I.T) IV-Sem./2017/10531

You might also like