Java moment

This commit is contained in:
2023-10-18 13:38:56 -05:00
parent 7da3542ede
commit f9babdde19
42 changed files with 1616 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>FileStream</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.filestream.FileStream</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,62 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.filestream;
/**
*
* @author caleb
*/
import java.io.*;
public class Copy {
/** Main method
@param args[0] for sourcefile
@param args[1] for target file
*/
public static void main(String[] args) throws IOException {
// Check command-line parameter usage
if (args.length != 2) {
System.out.println(
"Usage: java Copy sourceFile targetfile");
System.exit(1);
}
// Check if source file exists
File sourceFile = new File(args[0]);
if (!sourceFile.exists()) {
System.out.println("Source file " + args[0]
+ " does not exist");
System.exit(2);
}
// Check if target file exists
File targetFile = new File(args[1]);
if (targetFile.exists()) {
System.out.println("Target file " + args[1]
+ " already exists");
System.exit(3);
}
try (
// Create an input stream
BufferedInputStream input =
new BufferedInputStream(new FileInputStream(sourceFile));
// Create an output stream
BufferedOutputStream output =
new BufferedOutputStream(new FileOutputStream(targetFile));
) {
// Continuously read a byte from input and write it to output
int r, numberOfBytesCopied = 0;
while ((r = input.read()) != -1) {
output.write((byte)r);
numberOfBytesCopied++;
}
// Display the file size
System.out.println(numberOfBytesCopied + " bytes copied");
}
}
}

View File

@@ -0,0 +1,28 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.filestream;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author caleb
*/
public class FileChooser {
public static void main(String[] args)
{
// Using this process to invoke the constructor,
// JFileChooser points to user's default directory
JFileChooser j = new JFileChooser();
// Open the save dialog
j.showSaveDialog(null);
}
}

View File

@@ -0,0 +1,33 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.filestream;
import java.io.*;
/**
*
* @author caleb
*/
public class FileStream {
public static void main(String[] args) throws IOException {
try (
// Create an output stream to the file
FileOutputStream output = new FileOutputStream("temp.dat");
) {
// Output values to the file
for (int i = 1; i <= 10; i++)
output.write(i);
}
try (
// Create an input stream for the file
FileInputStream input = new FileInputStream("temp.dat");
) {
// Read values from the file
int value;
while ((value = input.read()) != -1)
System.out.print(value + " ");
}
}
}

View File

@@ -0,0 +1,44 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.filestream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
//import java.lang.Byte;
/**
*
* @author caleb
*/
public class ReadRawBytes {
public static void main(String[] args)
{
ArrayList<Byte> byteArrayList = new ArrayList<Byte>();
// Read raw bytes from file until 0xFF
try ( RandomAccessFile inout = new RandomAccessFile("test.dat", "r");) {
byte currentByte = 0x00;
for (int i = 0; i < inout.length(); i++) {
currentByte = inout.readByte();
if (currentByte != -1) {
byteArrayList.add(currentByte);
} else {
break;
}
}
} catch (IOException ex) {
System.out.println(ex);
}
byte[] byteArray = new byte[byteArrayList.size()];
for (int i = 0; i < byteArrayList.size(); i++) {
byteArray[i] = byteArrayList.get(i);
}
System.out.println("Data read back from file was: ");
String fileString = new String(byteArray, StandardCharsets.UTF_8);
System.out.println(fileString);
}
}

View File

@@ -0,0 +1,43 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.filestream;
import java.io.Serializable;
/**
*
* @author caleb
*/
public class Test implements Serializable {
private int x;
public Test(int x)
{
this.x = x;
}
/**
* Get the value of x
*
* @return the value of x
*/
public int getX()
{
return x;
}
/**
* Set the value of x
*
* @param x new value of x
*/
public void setX(int x)
{
this.x = x;
}
}

View File

@@ -0,0 +1,38 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.filestream;
/**
*
* @author caleb
*/
import java.io.*;
public class TestDataStream {
public static void main(String[] args) throws IOException {
try ( // Create an output stream for file temp.dat
DataOutputStream output =
new DataOutputStream(new FileOutputStream("temp.dat"));
) {
// Write student test scores to the file
output.writeUTF("John");
output.writeDouble(85.5);
output.writeUTF("Jim");
output.writeDouble(185.5);
output.writeUTF("George");
output.writeDouble(105.25);
}
try ( // Create an input stream for file temp.dat
DataInputStream input =
new DataInputStream(new FileInputStream("temp.dat"));
) {
// Read student test scores from the file
System.out.println(input.readUTF() + " " + input.readDouble());
System.out.println(input.readUTF() + " " + input.readDouble());
System.out.println(input.readUTF() + " " + input.readDouble());
}
}
}

View File

@@ -0,0 +1,55 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.filestream;
/**
*
* @author caleb
*/
import java.io.*;
public class TestObjectStreamForArray {
public static void main(String[] args)
throws ClassNotFoundException, IOException
{
int[] numbers = {1, 2, 3, 4, 5};
Test[] arTest = new Test[2];
arTest[0] = new Test(10);
arTest[0] = new Test(20);
String[] strings = {"John", "Susan", "Kim"};
try ( // Create an output stream for file array.dat
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("array.dat", true));) {
// Write arrays to the object output stream
output.writeObject(numbers);
output.writeObject(strings);
output.writeObject(arTest);
}
try ( // Create an input stream for file array.dat
ObjectInputStream input
= new ObjectInputStream(new FileInputStream("array.dat"));) {
int[] newNumbers = (int[]) (input.readObject());
String[] newStrings = (String[]) (input.readObject());
// Display arrays
for (int i = 0; i < newNumbers.length; i++) {
System.out.print(newNumbers[i] + " ");
}
System.out.println();
for (int i = 0; i < newStrings.length; i++) {
System.out.print(newStrings[i] + " ");
}
System.out.println();
for (int i = 0; i < arTest.length; i++) {
System.out.print(arTest[i] + " ");
}
}
}
}

View File

@@ -0,0 +1,75 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.filestream;
/**
*
* @author caleb
*/
import java.io.*;
public class TestRandomAccessFile {
public static void main(String[] args) throws IOException
{
try ( // Create a random access file
RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw");) {
// Clear the file to destroy the old contents if exists
inout.setLength(0);
// Write new integers to the file
for (int i = 0; i < 200; i++) {
inout.writeInt(i);
}
// Display the current length of the file
System.out.println("Current file length is " + inout.length());
// Retrieve the first number
inout.seek(0); // Move the file pointer to the beginning
System.out.println("The first number is " + inout.readInt());
// Retrieve the second number
inout.seek(1 * 4); // Move the file pointer to the second number
System.out.println("The second number is " + inout.readInt());
// Retrieve the tenth number
inout.seek(9 * 4); // Move the file pointer to the tenth number
System.out.println("The tenth number is " + inout.readInt());
// Modify the eleventh number
inout.writeInt(555);
// Append a new number
inout.seek(inout.length()); // Move the file pointer to the end
inout.writeInt(999);
// Display the new length
System.out.println("The new length is " + inout.length());
// Retrieve the new eleventh number
inout.seek(10 * 4); // Move the file pointer to the eleventh number
System.out.println("The eleventh number is " + inout.readInt());
inout.seek(0); // Move the pointer to the beginning of the file
int intOut;
int counter = 0;
while (true) {
try {
counter++;
intOut = inout.readInt();
System.out.print(intOut + " ");
if ((counter + 1) % 5 == 0) {
System.out.println();
}
} catch (EOFException ex) {
System.out.println();
System.out.println("We reached the end of the file!");
break;
}
}
}
}
}

View File

@@ -0,0 +1 @@


View File

@@ -0,0 +1 @@
This is another abritrary string of characters<72>