Posts Tagged File
Scanner to read various types of data from a file.
This example shows how to use scanner to read various type(like integer, double, boolean, string) of data from a file.
import java.util.*;
import java.lang.io.*;
class ScanMixed{
public static void main(String a[]) throws IOException {
int i;
double d;
boolean b;
String str;
//Write output to a file
FileWriter fout = new FileWriter("test.txt");
fout.write("Testing Scanner 10 12.2 one true two false");
fout.close();
FileReader fin= new FileReader("Test.txt");
Scanner src= new scanner(fin);
// Read to end.
while (src.hasNext()) {
if(src.hasNextInt()) {
i= src.nextInt();
System.out.println("int: "+ i);
}
else if(src.hasNextDouble()) {
d= src.nextDouble ();
System.out.println("double: "+ d);
}
else if(src.hasNextBoolean()) {
b=src.nextBoolean();
System.out.println("boolean: "+b);
}
else {
str =src.next();
System.out.println("String: "+ str);
}
}
fin.close();
}
}
Program to display complete information about a file: Perm, inode, acl bits.
Posted by admin in perl hacks on June 14th, 2009
#!/usr/bin/perl
use File::stat;
$pwd = (getpwuid($<))[1];
print "$pwd";
system "stty -echo";
print "Password: ";
chomp($word =
PROGRAM TO GENERATE 30000 RANDOM NUMBERS AND WRITE TO FILE RECORDS.TXT IN SORTED ORDER (USED INORDER TRAVERSAL FOR THIS).
Posted by admin in C++ Examples on June 14th, 2009
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
int total=0;
fstream file;
struct bintree
{
int data;
bintree *left,*right;
};
void inorder(bintree *node)
{
if(node->left!=NULL)
inorder(node->left);
file<<" "<<node->data;
if(node->right!=NULL)
inorder(node->right);
}
void makenode(bintree **garb,int num)
{
(*garb)->data=num;
(*garb)->left=NULL;
(*garb)->right=NULL;
}
void makebintree(bintree *garb,bintree **base)
{
if((*base)==NULL)
{
*base=garb;
total++;
return;
}
else if(garb->data==(*base)->data)
{
return;
}
else if(garb->data<(*base)->data)
makebintree(garb,&((*base)->left));
else
makebintree(garb,&((*base)->right));
}
void dump(bintree **node)
{
if(*node==NULL)
return;
if((*node)->left==NULL&&(*node)->right==NULL)
{
delete(*node);
*node=NULL;
}
else
{
dump(&((*node)->left));
dump(&((*node)->right));
dump(node);
}
}
void main()
{
bintree *garb,*root=NULL;
file.open("records.txt",ios::out);
while(total<30000)
{
garb=new bintree;
makenode(&garb,rand());
makebintree(garb,&root);
}
inorder(root);
dump(&root);
file.close();
}
PROGRAM TO GENERATE 20000 RANDOM KEYS AND WRITE TO FILE KEYS.TXT
Posted by admin in C++ Examples on June 14th, 2009
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
int total=0;
fstream file;
struct bintree
{
int data;
bintree *left,*right;
};
void makenode(bintree **garb,int num)
{
(*garb)->data=num;
(*garb)->left=NULL;
(*garb)->right=NULL;
}
void makebintree(bintree *garb,bintree **base)
{
if((*base)==NULL)
{
*base=garb;
total++;
file<<garb->data<<" ";
return;
}
else if(garb->data==(*base)->data)
{
return;
}
else if(garb->data<(*base)->data)
makebintree(garb,&((*base)->left));
else
makebintree(garb,&((*base)->right));
}
void dump(bintree **node)
{
if(*node==NULL)
return;
if((*node)->left==NULL&&(*node)->right==NULL)
{
delete(*node);
*node=NULL;
}
else
{
dump(&((*node)->left));
dump(&((*node)->right));
dump(node);
}
}
void main()
{
bintree *garb,*root=NULL;
file.open("keys.txt",ios::out);
while(total<20000)
{
garb=new bintree;
makenode(&garb,rand());
makebintree(garb,&root);
}
dump(&root);
file.close();
}


Recent Comments