0% found this document useful (0 votes)
1 views

File IO Solutions

The document contains four Java solutions for file input/output operations. Each solution demonstrates different functionalities such as writing user input to a file, reading from a file, performing calculations, and filtering words based on vowel presence. The code snippets include error handling for file operations, but some contain inappropriate language in the error messages.

Uploaded by

jetblackaqua957
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

File IO Solutions

The document contains four Java solutions for file input/output operations. Each solution demonstrates different functionalities such as writing user input to a file, reading from a file, performing calculations, and filtering words based on vowel presence. The code snippets include error handling for file operations, but some contain inappropriate language in the error messages.

Uploaded by

jetblackaqua957
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

FILE INPUT/OUTPUT/READ/WRITE SOLN

SOLUTION 1

import java.io.*;
import java.util.*;
class newstuff
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String filename= "output.txt";
String toadd=sc.nextLine();
try(FileWriter writer=new FileWriter(filename))
{
writer.write(toadd);
}
catch(IOException e)
{
System.out.println("Erro writing in this file");
}
int countdigit=0;
try(BufferedReader reader=new BufferedReader(new FileReader(filename)))
{
int ch;
while((ch=reader.read())!=-1)
{
if(Character.isDigit(ch))
{
countdigit++;
}
}
}
catch(IOException e)
{
System.out.println("CAnt do it man");return;
}
System.out.println(countdigit);
}
}

SOLITION 2

import java.util.*;
import java.io.*;
class filenewtranasfer
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String inputfile="data.txt";
String outputfile="converted.txt";

//Writing in input file mate


try(FileWriter writer=new FileWriter(inputfile))
{
double aa=sc.nextDouble();
writer.write(Double.toString(aa));
}
catch(IOException e)
{
System.out.println("ERROR NIGGA");
}
double converted=0.0;

//reading from input file and doing calulations


try(BufferedReader reader=new BufferedReader(new FileReader(inputfile)))
{
double bb=Double.parseDouble(reader.readLine());
converted=bb*5.0/18.0;
}
catch(IOException e)
{
System.out.println("ERROR NIGGA");
}

//writing back in output file


try(FileWriter writer=new FileWriter(outputfile))
{
writer.write(String.format("%.2f m/s",converted));
}
catch(IOException e)
{
System.out.println("ERROR NIGGA");
}
System.out.printf("%.2f m/s\n",converted);
}
}

SOLUTION 3

import java.io.*;
import java.util.*;
class wtf
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++)
{
int aa=sc.nextInt();
arr[i]=aa;
}
String outputfile="output.txt";
try(FileWriter writer= new FileWriter(outputfile))
{
String check;
for(int j=0;j<n;j++)
{
if(arr[j]%5==0)
{
check="Plant "+arr[j]+" gram seed";
}
else
{
check="Change "+arr[j]+" gram seed";
}
writer.write(check+"\n");
System.out.println(check);
}
}
catch(Exception e)
{
System.out.println("error nigga");
}
}
}

SOLUTON 4

import java.io.*;
import java.util.*;
class wtf
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String workfile="ouput.txt";
try(FileWriter writer=new FileWriter(workfile))
{
String inpu=sc.nextLine();
writer.write(inpu);
}
catch(Exception e)
{
System.out.println("Error nigga");
}
try(BufferedReader reader=new BufferedReader(new FileReader(workfile)))
{
String line=reader.readLine();
String[] word=line.split("\\s+");
for (String words:word)
{
if(!words.toLowerCase().matches(".*[aeiou].*"))
{
System.out.println(words);
}
}
}
catch(Exception e)
{
System.out.println("Error nigga");
}
}
}

You might also like