The sidewalk is designed for pedestrians going pedestrian speeds. Therefore, motorists are expecting pedestrians, not cyclists, and will turn in front of a cyclist (that they think is going much slower, as untrained people are horrible judges of speed), causing a right hook collision. The only time I will use the sidewalk is if I'm going at pedestrian speeds, and that's usually to access on-sidewalk bicycle parking.
If the lane is wide enough to safely share with car traffic, does not have parked cars along the right side, and does not have a significant number of driveways, riding in the right side of the lane (allowing at least 3 feet to both sides) is relatively safe.
Otherwise, take the lane. Assuming you're in a US state that follows the Uniform Traffic Code, you have the legal right to take the lane. The laws in most states require a bicycle rider to rider "as far to the right as practicable", but precedent has set that, if the lane is any less than 14 feet wide (and I do mean the lane - this doesn't include space in which cars are parked - speaking of which, don't weave in and out of parked cars), the lane is yours.
Yes, you'll annoy drivers. Better to annoy them than for them to never notice you.
Also, this is where equipment comes in. Run a mirror, so that you can see traffic that's behind you easily - you can get handlebar, helmet, or eyeglass mount mirrors. If you see cars backing up behind you and are on a 2-lane road, you can help motorists get past you - watch for oncoming traffic, and if the way is clear, wave motorists past. If the way isn't clear and doesn't appear to be clearing any time soon, you may consider pulling off in a safe place to allow motorists to pass you.
While I'm at it... lighting is important. Good quality lights both front and rear, to be seen by, and to see with, are crucial if you're riding at night. And, avoid dark clothing.